forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microsoft_client.go
186 lines (159 loc) · 4.32 KB
/
microsoft_client.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
package obj
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"github.com/pachyderm/pachyderm/src/client/pkg/grpcutil"
"github.com/Azure/azure-sdk-for-go/storage"
)
type microsoftClient struct {
blobClient storage.BlobStorageClient
container string
}
func newMicrosoftClient(container string, accountName string, accountKey string) (*microsoftClient, error) {
client, err := storage.NewBasicClient(
accountName,
accountKey,
)
if err != nil {
return nil, err
}
return µsoftClient{
blobClient: client.GetBlobService(),
container: container,
}, nil
}
func (c *microsoftClient) Writer(name string) (io.WriteCloser, error) {
writer, err := newMicrosoftWriter(c, name)
if err != nil {
return nil, err
}
return newBackoffWriteCloser(c, writer), nil
}
func (c *microsoftClient) Reader(name string, offset uint64, size uint64) (io.ReadCloser, error) {
byteRange := byteRange(offset, size)
var reader io.ReadCloser
var err error
if byteRange == "" {
reader, err = c.blobClient.GetBlob(c.container, name)
} else {
reader, err = c.blobClient.GetBlobRange(c.container, name, byteRange, nil)
}
if err != nil {
return nil, err
}
return newBackoffReadCloser(c, reader), nil
}
func (c *microsoftClient) Delete(name string) error {
return c.blobClient.DeleteBlob(c.container, name, nil)
}
func (c *microsoftClient) Walk(name string, fn func(name string) error) error {
// See Azure docs for what `marker` does:
// https://docs.microsoft.com/en-us/rest/api/storageservices/List-Blobs?redirectedfrom=MSDN
var marker string
for {
blobList, err := c.blobClient.ListBlobs(c.container, storage.ListBlobsParameters{
Prefix: name,
Marker: marker,
})
if err != nil {
return err
}
for _, file := range blobList.Blobs {
if err := fn(file.Name); err != nil {
return err
}
}
// NextMarker is empty when all results have been returned
if blobList.NextMarker == "" {
break
}
marker = blobList.NextMarker
}
return nil
}
func (c *microsoftClient) Exists(name string) bool {
exists, _ := c.blobClient.BlobExists(c.container, name)
return exists
}
func (c *microsoftClient) IsRetryable(err error) (ret bool) {
microsoftErr, ok := err.(storage.AzureStorageServiceError)
if !ok {
return false
}
return microsoftErr.StatusCode >= 500
}
func (c *microsoftClient) IsNotExist(err error) bool {
microsoftErr, ok := err.(storage.AzureStorageServiceError)
if !ok {
return false
}
return microsoftErr.StatusCode == 404
}
func (c *microsoftClient) IsIgnorable(err error) bool {
return false
}
type microsoftWriter struct {
container string
blob string
blobClient storage.BlobStorageClient
}
func newMicrosoftWriter(client *microsoftClient, name string) (*microsoftWriter, error) {
// create container
_, err := client.blobClient.CreateContainerIfNotExists(client.container, storage.ContainerAccessTypePrivate)
if err != nil {
return nil, err
}
// create blob
err = client.blobClient.CreateBlockBlob(client.container, name)
if err != nil {
return nil, err
}
return µsoftWriter{
container: client.container,
blob: name,
blobClient: client.blobClient,
}, nil
}
func (w *microsoftWriter) Write(b []byte) (int, error) {
blockList, err := w.blobClient.GetBlockList(w.container, w.blob, storage.BlockListTypeAll)
if err != nil {
return 0, err
}
blocksLen := len(blockList.CommittedBlocks)
amendList := []storage.Block{}
for _, v := range blockList.CommittedBlocks {
amendList = append(amendList, storage.Block{v.Name, storage.BlockStatusCommitted})
}
inputSourceReader := bytes.NewReader(b)
chunk := grpcutil.GetBuffer()
defer grpcutil.PutBuffer(chunk)
for {
n, err := inputSourceReader.Read(chunk)
if err == io.EOF {
break
}
if err != nil {
return 0, err
}
blockID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%011d\n", blocksLen)))
data := chunk[:n]
err = w.blobClient.PutBlock(w.container, w.blob, blockID, data)
if err != nil {
return 0, err
}
// add current uncommitted block to temporary block list
amendList = append(amendList, storage.Block{blockID, storage.BlockStatusUncommitted})
blocksLen++
}
// update block list to blob committed block list.
err = w.blobClient.PutBlockList(w.container, w.blob, amendList)
if err != nil {
return 0, err
}
return len(b), nil
}
func (w *microsoftWriter) Close() error {
return nil
}