forked from decentralized-identity/go-ipfs-ds-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_data_store.go
342 lines (294 loc) · 9.92 KB
/
azure_data_store.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package azureds
import (
"bytes"
"context"
"fmt"
"net/url"
"os"
"strings"
"github.com/Azure/azure-storage-blob-go/azblob"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
)
// AzureStorage is a storage representation
type AzureStorage struct {
Config
}
// Config representation for all info needed
type Config struct {
AccountName string
AccountKey string
ContainerName string
FolderName string
}
// NewAzureDatastore creates an AzureDatastore
func NewAzureDatastore(conf Config) (*AzureStorage, error) {
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
if err != nil {
return nil, err
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
baseUrl, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", conf.AccountName))
serviceURL := azblob.NewServiceURL(*baseUrl, pipeline)
containerURL := serviceURL.NewContainerURL(conf.ContainerName)
containerURL.Create(context.Background(), azblob.Metadata{}, "")
return &AzureStorage{
Config: conf,
}, nil
}
// GetBlockURL returns the block url of a given key
func (storage *AzureStorage) GetBlockURL(key string) (*azblob.BlockBlobURL, error) {
// From the Azure portal, get your Storage account blob service URL endpoint.
accountName := storage.Config.AccountName
accountKey := storage.Config.AccountKey
containerName := storage.Config.ContainerName
folderName := storage.Config.FolderName
fmt.Printf("accountName:%s\n", accountName)
fmt.Printf("accountKey:%s\n", accountKey)
fmt.Printf("containerName:%s\n", containerName)
fmt.Printf("folderName:%s\n", folderName)
var string = ""
// Create a ContainerURL object that wraps a soon-to-be-created blob's URL and a default pipeline.
if folderName == "" {
string = fmt.Sprintf("https://%s.blob.core.windows.net/%s%s", accountName, containerName, key)
} else {
string = fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s%s", accountName, containerName, folderName, key)
}
fmt.Printf("u.string:%s\n", string)
u, _ := url.Parse(string)
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
fmt.Printf(err.Error())
return nil, err
}
fmt.Printf("azblob.NewSharedKeyCredential ok\n")
blobURL := azblob.NewBlockBlobURL(*u, azblob.NewPipeline(credential, azblob.PipelineOptions{}))
fmt.Printf("azblob.NewBlockBlobURL ok. %+v\n", blobURL)
return &blobURL, nil
}
// Put adds a key value pair to the storage
func (storage *AzureStorage) Put(ctx context.Context, k ds.Key, value []byte) error {
fmt.Printf("azurestorage put:%s\n", k.String())
fmt.Printf("azurestorage put:%d\n", len(value))
blobURL, err := storage.GetBlockURL(k.String())
if err != nil {
return err
}
// Create a blob with metadata (string key/value pairs)
// NOTE: Metadata key names are always converted to lowercase before being sent to the Storage Service.
// Therefore, you should always use lowercase letters; especially when querying a map for a metadata key.
creatingApp, _ := os.Executable()
fmt.Printf("azurestorage creatingApp:%s\n", creatingApp)
_, err = blobURL.Upload(ctx, bytes.NewReader(value), azblob.BlobHTTPHeaders{},
azblob.Metadata{"author": "dqfs", "app": creatingApp}, azblob.BlobAccessConditions{}, azblob.DefaultAccessTier, nil, azblob.ClientProvidedKeyOptions{}, azblob.ImmutabilityPolicyOptions{})
if err != nil {
return err
}
return nil
}
// Sync is unimplemented
func (storage *AzureStorage) Sync(ctx context.Context, prefix ds.Key) error {
return nil
}
// Get gets the data from the desired key
func (storage *AzureStorage) Get(ctx context.Context, k ds.Key) ([]byte, error) {
blobURL, err := storage.GetBlockURL(k.String())
if err != nil {
return nil, err
}
response, err := blobURL.Download(ctx, 0, 0, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
if err != nil {
if stgErr, ok := err.(azblob.StorageError); ok {
if stgErr.ServiceCode() == azblob.ServiceCodeBlobNotFound {
return nil, ds.ErrNotFound
}
}
return nil, err
}
blobData := &bytes.Buffer{}
reader := response.Body(azblob.RetryReaderOptions{})
blobData.ReadFrom(reader)
reader.Close() // The client must close the response body when finished with it
return blobData.Bytes(), nil
}
// Has checks if the given key exists
func (storage *AzureStorage) Has(ctx context.Context, k ds.Key) (exists bool, err error) {
blobURL, err := storage.GetBlockURL(k.String())
if err != nil {
return false, err
}
_, err = blobURL.GetBlockList(ctx, azblob.BlockListCommitted, azblob.LeaseAccessConditions{})
if err != nil {
if stgErr, ok := err.(azblob.StorageError); ok &&
stgErr.ServiceCode() == azblob.ServiceCodeBlobNotFound {
return false, nil
}
return false, err
}
return true, nil
}
// GetSize gets the size of the specified key
func (storage *AzureStorage) GetSize(ctx context.Context, k ds.Key) (size int, err error) {
blobURL, err := storage.GetBlockURL(k.String())
if err != nil {
return 0, err
}
blockList, err := blobURL.GetBlockList(ctx, azblob.BlockListCommitted, azblob.LeaseAccessConditions{})
if err != nil {
if stgErr, ok := err.(azblob.StorageError); ok &&
stgErr.ServiceCode() == azblob.ServiceCodeBlobNotFound {
return 0, ds.ErrNotFound
}
return 0, err
}
return int(blockList.BlobContentLength()), nil
}
// Delete deletes the specified key
func (storage *AzureStorage) Delete(ctx context.Context, k ds.Key) error {
fmt.Printf("\n\nDelete\n")
fmt.Printf("Delete %s\n", k.String())
blobURL, err := storage.GetBlockURL(k.String())
if err != nil {
return err
}
_, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
if err != nil {
fmt.Printf("blob delete err:%+v\n", err)
if stgErr, ok := err.(azblob.StorageError); ok &&
stgErr.ServiceCode() == azblob.ServiceCodeBlobNotFound {
fmt.Printf("blob delete err blob not found\n")
return ds.ErrNotFound
}
return err
}
return nil
}
// Query returns a dsq result
func (storage *AzureStorage) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
fmt.Printf("Azure Query\nq.String=%v\n", q.String())
fmt.Printf("q.offset=%v\n", q.Offset)
fmt.Printf("q.prefix=%v\n", q.Prefix)
fmt.Printf("q.returnSizes=%v\n", q.ReturnsSizes)
if q.Orders != nil || q.Filters != nil {
return nil, fmt.Errorf("Azure: filters or orders are not supported")
}
accountName := storage.Config.AccountName
accountKey := storage.Config.AccountKey
containerName := storage.Config.ContainerName
folderName := storage.Config.FolderName
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
fmt.Println("Invalid credentials provided")
return nil, fmt.Errorf("Azure: Invalid credentials provided")
}
limit := q.Limit + q.Offset
if limit == 0 || limit > 1000 {
limit = 1000
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
baseUrl, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/", accountName))
serviceURL := azblob.NewServiceURL(*baseUrl, pipeline)
containerURL := serviceURL.NewContainerURL(containerName)
var string = ""
if folderName == "" {
string = q.Prefix
} else {
string = folderName + q.Prefix
}
string = strings.TrimPrefix(string, "/")
string = string + "/"
fmt.Printf("prefix string=%s\n", string)
listBlob, err := containerURL.ListBlobsHierarchySegment(context.Background(), azblob.Marker{}, "/", azblob.ListBlobsSegmentOptions{Prefix: string})
if err != nil {
fmt.Printf("Error listing blobs in directory: %v\n", err)
return nil, fmt.Errorf("Azure: Error listing blobs in directory")
}
entry := dsq.Entry{}
fmt.Printf("Azure listblob\n")
index := q.Offset
fmt.Printf("len(listBlob.Segment.BlobItems):%d\n", len(listBlob.Segment.BlobItems))
length := len(listBlob.Segment.BlobItems)
nextValue := func() (dsq.Result, bool) {
fmt.Printf("nextValue enter, length=%d\n", length)
if len(listBlob.Segment.BlobItems) == 0 {
return dsq.Result{}, false
}
if length <= 0 {
return dsq.Result{}, false
}
//for _, blob := range listBlob.Segment.BlobItems {
blobName := listBlob.Segment.BlobItems[index].Name
blobSize := listBlob.Segment.BlobItems[index].Properties.ContentLength
fmt.Printf("length = %d\n", length)
length = length - 1
var string = "listbolb:" + ds.NewKey(blobName).String() + "\n"
fmt.Println(string)
if strings.HasPrefix(ds.NewKey(blobName).String(), q.Prefix) {
var string = "key:" + ds.NewKey(blobName).String() + "\n"
fmt.Println(string)
fmt.Printf("key size:%d\n", int(*blobSize))
entry = dsq.Entry{
Key: ds.NewKey(blobName).String(),
Size: int(*blobSize),
Value: nil,
}
fmt.Printf("dsq entry ok\n")
index++
return dsq.Result{Entry: entry}, true
}
return dsq.Result{}, false
}
fmt.Printf("dsq ResultsFromIterator enter\n")
return dsq.ResultsFromIterator(q, dsq.Iterator{
Close: func() error {
return nil
},
Next: nextValue,
}), nil
}
// Close is not implemented
func (storage *AzureStorage) Close() error {
return nil
}
type op struct {
delete bool
value []byte
}
// basicBatch implements
type batchOp struct {
val []byte
delete bool
}
type azureBatch struct {
storage *AzureStorage
ops map[string]batchOp
}
// Batch returns a batch struct that can take more ops or be committed
func (storage *AzureStorage) Batch(ctx context.Context) (ds.Batch, error) {
return &azureBatch{
storage: storage,
ops: make(map[string]batchOp),
}, nil
}
func (batch *azureBatch) Put(ctx context.Context, key ds.Key, val []byte) error {
batch.ops[key.String()] = batchOp{val: val, delete: false}
return nil
}
func (batch *azureBatch) Delete(ctx context.Context, key ds.Key) error {
batch.ops[key.String()] = batchOp{val: nil, delete: true}
return nil
}
func (batch *azureBatch) Commit(ctx context.Context) error {
var err error
for k, op := range batch.ops {
if op.delete {
err = batch.storage.Delete(ctx, ds.NewKey(k))
} else {
err = batch.storage.Put(ctx, ds.NewKey(k), op.val)
}
if err != nil {
break
}
}
return err
}