-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
blobs.go
348 lines (285 loc) · 8.88 KB
/
blobs.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
343
344
345
346
347
348
package storage
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
)
const pollingInterval = time.Second * 15
type BlobUpload struct {
Client *blobs.Client
AccountName string
BlobName string
ContainerName string
BlobType string
ContentType string
MetaData map[string]string
Parallelism int
Size int
Source string
SourceContent string
SourceUri string
}
func (sbu BlobUpload) Create(ctx context.Context) error {
blobType := strings.ToLower(sbu.BlobType)
if blobType == "append" {
if sbu.Source != "" || sbu.SourceContent != "" || sbu.SourceUri != "" {
return fmt.Errorf("A source cannot be specified for an Append blob")
}
return sbu.createEmptyAppendBlob(ctx)
}
if blobType == "block" {
if sbu.SourceUri != "" {
return sbu.copy(ctx)
}
if sbu.SourceContent != "" {
return sbu.uploadBlockBlobFromContent(ctx)
}
if sbu.Source != "" {
return sbu.uploadBlockBlob(ctx)
}
return sbu.createEmptyBlockBlob(ctx)
}
if blobType == "page" {
if sbu.SourceUri != "" {
return sbu.copy(ctx)
}
if sbu.SourceContent != "" {
return fmt.Errorf("`source_content` cannot be specified for a Page blob")
}
if sbu.Source != "" {
return sbu.uploadPageBlob(ctx)
}
return sbu.createEmptyPageBlob(ctx)
}
return fmt.Errorf("Unsupported Blob Type: %q", blobType)
}
func (sbu BlobUpload) copy(ctx context.Context) error {
input := blobs.CopyInput{
CopySource: sbu.SourceUri,
MetaData: sbu.MetaData,
}
if err := sbu.Client.CopyAndWait(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input, pollingInterval); err != nil {
return fmt.Errorf("Error copy/waiting: %s", err)
}
return nil
}
func (sbu BlobUpload) createEmptyAppendBlob(ctx context.Context) error {
input := blobs.PutAppendBlobInput{
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutAppendBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutAppendBlob: %s", err)
}
return nil
}
func (sbu BlobUpload) createEmptyBlockBlob(ctx context.Context) error {
input := blobs.PutBlockBlobInput{
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutBlockBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutBlockBlob: %s", err)
}
return nil
}
func (sbu BlobUpload) uploadBlockBlobFromContent(ctx context.Context) error {
tmpFile, err := ioutil.TempFile(os.TempDir(), "upload-")
if err != nil {
return fmt.Errorf("Error creating temporary file: %s", err)
}
defer os.Remove(tmpFile.Name())
if _, err = tmpFile.Write([]byte(sbu.SourceContent)); err != nil {
return fmt.Errorf("Error writing Source Content to Temp File: %s", err)
}
defer tmpFile.Close()
sbu.Source = tmpFile.Name()
return sbu.uploadBlockBlob(ctx)
}
func (sbu BlobUpload) uploadBlockBlob(ctx context.Context) error {
file, err := os.Open(sbu.Source)
if err != nil {
return fmt.Errorf("Error opening: %s", err)
}
defer file.Close()
input := blobs.PutBlockBlobInput{
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if err := sbu.Client.PutBlockBlobFromFile(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, file, input); err != nil {
return fmt.Errorf("Error PutBlockBlobFromFile: %s", err)
}
return nil
}
func (sbu BlobUpload) createEmptyPageBlob(ctx context.Context) error {
if sbu.Size == 0 {
return fmt.Errorf("`size` cannot be zero for a page blob")
}
input := blobs.PutPageBlobInput{
BlobContentLengthBytes: int64(sbu.Size),
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutPageBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutPageBlob: %s", err)
}
return nil
}
func (sbu BlobUpload) uploadPageBlob(ctx context.Context) error {
if sbu.Size != 0 {
return fmt.Errorf("`size` cannot be set for an uploaded page blob")
}
// determine the details about the file
file, err := os.Open(sbu.Source)
if err != nil {
return fmt.Errorf("Error opening source file for upload %q: %s", sbu.Source, err)
}
defer file.Close()
// TODO: all of this ultimately can be moved into Giovanni
info, err := file.Stat()
if err != nil {
return fmt.Errorf("Could not stat file %q: %s", file.Name(), err)
}
fileSize := info.Size()
// first let's create a file of the specified file size
input := blobs.PutPageBlobInput{
BlobContentLengthBytes: fileSize,
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutPageBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutPageBlob: %s", err)
}
if err := sbu.pageUploadFromSource(ctx, file, fileSize); err != nil {
return fmt.Errorf("Error creating storage blob on Azure: %s", err)
}
return nil
}
// TODO: move below here into Giovanni
type storageBlobPage struct {
offset int64
section *io.SectionReader
}
func (sbu BlobUpload) pageUploadFromSource(ctx context.Context, file io.ReaderAt, fileSize int64) error {
workerCount := sbu.Parallelism * runtime.NumCPU()
// first we chunk the file and assign them to 'pages'
pageList, err := sbu.storageBlobPageSplit(file, fileSize)
if err != nil {
return fmt.Errorf("Error splitting source file %q into pages: %s", sbu.Source, err)
}
// finally we upload the contents of said file
pages := make(chan storageBlobPage, len(pageList))
errors := make(chan error, len(pageList))
wg := &sync.WaitGroup{}
wg.Add(len(pageList))
total := int64(0)
for _, page := range pageList {
total += page.section.Size()
pages <- page
}
close(pages)
for i := 0; i < workerCount; i++ {
go sbu.blobPageUploadWorker(ctx, blobPageUploadContext{
blobSize: fileSize,
pages: pages,
errors: errors,
wg: wg,
})
}
wg.Wait()
if len(errors) > 0 {
return fmt.Errorf("Error while uploading source file %q: %s", sbu.Source, <-errors)
}
return nil
}
const (
minPageSize int64 = 4 * 1024
// TODO: investigate whether this can be bumped to 100MB with the new API
maxPageSize int64 = 4 * 1024 * 1024
)
func (sbu BlobUpload) storageBlobPageSplit(file io.ReaderAt, fileSize int64) ([]storageBlobPage, error) {
// whilst the file Size can be any arbitrary Size, it must be uploaded in fixed-Size pages
blobSize := fileSize
if fileSize%minPageSize != 0 {
blobSize = fileSize + (minPageSize - (fileSize % minPageSize))
}
emptyPage := make([]byte, minPageSize)
type byteRange struct {
offset int64
length int64
}
var nonEmptyRanges []byteRange
var currentRange byteRange
for i := int64(0); i < blobSize; i += minPageSize {
pageBuf := make([]byte, minPageSize)
if _, err := file.ReadAt(pageBuf, i); err != nil && err != io.EOF {
return nil, fmt.Errorf("Could not read chunk at %d: %s", i, err)
}
if bytes.Equal(pageBuf, emptyPage) {
if currentRange.length != 0 {
nonEmptyRanges = append(nonEmptyRanges, currentRange)
}
currentRange = byteRange{
offset: i + minPageSize,
}
} else {
currentRange.length += minPageSize
if currentRange.length == maxPageSize || (currentRange.offset+currentRange.length == blobSize) {
nonEmptyRanges = append(nonEmptyRanges, currentRange)
currentRange = byteRange{
offset: i + minPageSize,
}
}
}
}
var pages []storageBlobPage
for _, nonEmptyRange := range nonEmptyRanges {
pages = append(pages, storageBlobPage{
offset: nonEmptyRange.offset,
section: io.NewSectionReader(file, nonEmptyRange.offset, nonEmptyRange.length),
})
}
return pages, nil
}
type blobPageUploadContext struct {
blobSize int64
pages chan storageBlobPage
errors chan error
wg *sync.WaitGroup
}
func (sbu BlobUpload) blobPageUploadWorker(ctx context.Context, uploadCtx blobPageUploadContext) {
for page := range uploadCtx.pages {
start := page.offset
end := page.offset + page.section.Size() - 1
if end > uploadCtx.blobSize-1 {
end = uploadCtx.blobSize - 1
}
size := end - start + 1
chunk := make([]byte, size)
if _, err := page.section.Read(chunk); err != nil && err != io.EOF {
uploadCtx.errors <- fmt.Errorf("Error reading source file %q at offset %d: %s", sbu.Source, page.offset, err)
uploadCtx.wg.Done()
continue
}
input := blobs.PutPageUpdateInput{
StartByte: start,
EndByte: end,
Content: chunk,
}
if _, err := sbu.Client.PutPageUpdate(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
uploadCtx.errors <- fmt.Errorf("Error writing page at offset %d for file %q: %s", page.offset, sbu.Source, err)
uploadCtx.wg.Done()
continue
}
uploadCtx.wg.Done()
}
}