-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
244 lines (210 loc) · 6.75 KB
/
azure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"context"
"fmt"
"io"
"log"
"net/url"
"os"
"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/spf13/cobra"
"gocloud.dev/blob"
"gocloud.dev/blob/azureblob"
)
const (
accountName azureblob.AccountName = "ENTER_YOUR_ACCOUNT_NAME"
accountKey azureblob.AccountKey = "ENTER_YOUR_KEY"
)
var (
// Global variables
ctx context.Context
credential *azblob.SharedKeyCredential
pline pipeline.Pipeline
// Flags
containerName string
blobKey string
blobValue string
blobPrefix string
// Commands
rootCmd = &cobra.Command{
Use: "azure",
Short: "Interact with azure using the azure CLI",
}
createContainerCmd = &cobra.Command{
Use: "create-container",
Short: "Create an azure container",
Run: func(cmd *cobra.Command, args []string) {
// From the Azure portal, get your storage account blob service URL endpoint.
URL, _ := url.Parse(
fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
// Create a ContainerURL object that wraps the container URL and a request
// pipeline to make requests.
containerURL := azblob.NewContainerURL(*URL, pline)
fmt.Printf("Creating a container named %q\n", containerName)
_, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Successfully created container %q\n", containerName)
},
}
deleteContainerCmd = &cobra.Command{
Use: "delete-container",
Short: "Delete an azure container",
Run: func(cmd *cobra.Command, args []string) {
// From the Azure portal, get your storage account blob service URL endpoint.
URL, _ := url.Parse(
fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
// Create a ContainerURL object that wraps the container URL and a request
// pipeline to make requests.
containerURL := azblob.NewContainerURL(*URL, pline)
fmt.Printf("Deleting a container named %q\n", containerName)
_, err := containerURL.Delete(ctx, azblob.ContainerAccessConditions{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Successfully deleted container %q\n", containerName)
},
}
writeCmd = &cobra.Command{
Use: "write",
Short: "Write to a blob",
Run: func(cmd *cobra.Command, args []string) {
// Check if valid flags
if blobKey == "" {
log.Fatal(fmt.Errorf(`flag "--blob-key" should be set`))
}
if blobValue == "" {
log.Fatal(fmt.Errorf(`flag "--blob-value" should be set`))
}
// Create a *blob.Bucket.
// The credential Option is required if you're going to use blob.SignedURL.
bucket, err := azureblob.OpenBucket(ctx, pline, accountName, containerName,
&azureblob.Options{Credential: credential})
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Write
w, err := bucket.NewWriter(ctx, blobKey, nil)
if err != nil {
log.Fatal(err)
}
_, err = fmt.Fprintln(w, blobValue)
if err != nil {
log.Fatal(err)
}
err = w.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Successfully written %q to %q\n", blobValue, blobKey)
},
}
readCmd = &cobra.Command{
Use: "read",
Short: "Read from a blob",
Run: func(cmd *cobra.Command, args []string) {
// Check if valid flags
if blobKey == "" {
log.Fatal(fmt.Errorf(`flag "--blob-key" should be set`))
}
// Create a *blob.Bucket.
// The credential Option is required if you're going to use blob.SignedURL.
bucket, err := azureblob.OpenBucket(ctx, pline, accountName, containerName,
&azureblob.Options{Credential: credential})
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Open the key blobKey for reading with the default options.
r, err := bucket.NewReader(ctx, blobKey, nil)
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Readers also have a limited view of the blob's metadata.
fmt.Println("Content-Type:", r.ContentType())
fmt.Println()
// Copy from the reader to stdout.
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatal(err)
}
fmt.Printf("Successfully read from %q\n", blobKey)
},
}
listCmd = &cobra.Command{
Use: "list",
Short: "List from a blob with (or without) a prefix",
Run: func(cmd *cobra.Command, args []string) {
// Create a *blob.Bucket.
// The credential Option is required if you're going to use blob.SignedURL.
bucket, err := azureblob.OpenBucket(ctx, pline, accountName, containerName,
&azureblob.Options{Credential: credential})
if err != nil {
log.Fatal(err)
}
defer bucket.Close()
// Create a prefixed bucket
bucket = blob.PrefixedBucket(bucket, blobPrefix)
defer bucket.Close()
// list lists files in b starting with prefix. It uses the delimiter "/",
// and recurses into "directories", adding 2 spaces to indent each time.
// It will list the blobs created above because fileblob is strongly
// consistent, but is not guaranteed to work on all services.
var list func(context.Context, *blob.Bucket, string, string)
list = func(ctx context.Context, b *blob.Bucket, prefix, indent string) {
iter := b.List(&blob.ListOptions{
Delimiter: "/",
Prefix: prefix,
})
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s%s\n", indent, obj.Key)
if obj.IsDir {
list(ctx, b, obj.Key, indent+" ")
}
}
}
list(ctx, bucket, "", "")
fmt.Printf("Successfully listed from %q\n", blobPrefix)
},
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
// Add flags
rootCmd.PersistentFlags().StringVar(&containerName, "container-name", "default-container-name", "indicate a name of the container")
writeCmd.PersistentFlags().StringVar(&blobKey, "blob-key", "", "indicate a blob key for writing")
writeCmd.PersistentFlags().StringVar(&blobValue, "blob-value", "", "indicate a value you want to write to a given blob-key")
readCmd.PersistentFlags().StringVar(&blobKey, "blob-key", "", "indicate a blob key for writing")
listCmd.PersistentFlags().StringVar(&blobPrefix, "blob-prefix", "", "indicate a blob prefix to read from subdirectories")
// Add commands
rootCmd.AddCommand(createContainerCmd)
rootCmd.AddCommand(deleteContainerCmd)
rootCmd.AddCommand(writeCmd)
rootCmd.AddCommand(readCmd)
rootCmd.AddCommand(listCmd)
// Init azure
// Create a credentials object.
ctx = context.Background()
credential, err := azureblob.NewCredential(accountName, accountKey)
if err != nil {
log.Fatal(err)
}
// Create a Pipeline, using whatever PipelineOptions you need.
pline = azureblob.NewPipeline(credential, azblob.PipelineOptions{})
}
func main() {
Execute()
}