-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazureblob.go
147 lines (137 loc) · 3.29 KB
/
azureblob.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
package objectstore
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
type azureBlob struct {
basepath string
}
func newAzureBlob(basepath string) *azureBlob {
return &azureBlob{basepath: basepath}
}
func (ab *azureBlob) Read(name string) ([]byte, error) {
account, container, prefix, err := parseBlobPath(ab.basepath)
if err != nil {
return []byte{}, err
}
object := joinPath(prefix, name)
client, err := getBlobClient(account)
if err != nil {
return []byte{}, err
}
ctx := context.Background()
reader, err := client.DownloadStream(ctx, container, object, nil)
if err != nil {
return []byte{}, err
}
rs := reader.Body
stream := streaming.NewResponseProgress(
rs,
func(bytesTransferred int64) {},
)
defer func(stream io.ReadCloser) {
err := stream.Close()
if err != nil {
log.Fatal(err)
}
}(stream)
return io.ReadAll(stream)
}
func (ab *azureBlob) Write(name string, data []byte) error {
account, container, prefix, err := parseBlobPath(ab.basepath)
if err != nil {
return err
}
object := joinPath(prefix, name)
client, err := getBlobClient(account)
if err != nil {
return err
}
ctx := context.Background()
_, err = client.UploadBuffer(ctx, container, object, data, nil)
return err
}
func (ab *azureBlob) Delete(name string) error {
account, container, prefix, err := parseBlobPath(ab.basepath)
if err != nil {
return err
}
object := joinPath(prefix, name)
client, err := getBlobClient(account)
if err != nil {
return err
}
ctx := context.Background()
_, err = client.DeleteBlob(ctx, container, object, nil)
return err
}
func (ab *azureBlob) List() ([]string, error) {
files := []string{}
account, container, prefix, err := parseBlobPath(ab.basepath)
if err != nil {
return files, err
}
client, err := getBlobClient(account)
if err != nil {
return files, err
}
ctx := context.Background()
var nextMarker *string
for {
options := &azblob.ListBlobsFlatOptions{
Prefix: &prefix,
Marker: nextMarker,
}
p := client.NewListBlobsFlatPager(container, options)
data, err := p.NextPage(ctx)
if err != nil {
return files, err
}
for _, item := range data.Segment.BlobItems {
files = append(files, *item.Name)
}
if data.NextMarker != nil && *data.NextMarker != "" {
nextMarker = data.NextMarker
} else {
break
}
}
return files, err
}
func getBlobClient(account string) (*azblob.Client, error) {
accountKey, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_KEY")
if !ok {
return nil, errors.New("AZURE_STORAGE_ACCOUNT_KEY could not be found")
}
credential, err := azblob.NewSharedKeyCredential(account, accountKey)
if err != nil {
return nil, err
}
accountPath := fmt.Sprintf("https://%s.blob.core.windows.net/", account)
client, err := azblob.NewClientWithSharedKeyCredential(accountPath, credential, nil)
if err != nil {
return nil, err
}
return client, nil
}
func parseBlobPath(path string) (account, container, prefix string, err error) {
uri, err := url.Parse(path)
if err != nil {
return
}
fragments := strings.SplitN(strings.TrimPrefix(uri.Path, "/"), "/", 2)
if len(fragments) > 1 {
prefix = fragments[1]
}
account = uri.Host
container = strings.Trim(fragments[0], "/")
return
}