-
Notifications
You must be signed in to change notification settings - Fork 159
/
s3sync.go
482 lines (433 loc) · 12.4 KB
/
s3sync.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright(c) 2017-2018 Zededa, Inc.
// All rights reserved.
package awsutil
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/lf-edge/eve/libs/zedUpload/types"
)
// CustomReader contains the details of Chunks being downloaded
type CustomReader struct {
fp *os.File
name string
upSize types.UpdateStats
prgNotify types.StatsNotifChan
}
func (r *CustomReader) Read(p []byte) (int, error) {
n, err := r.fp.Read(p)
if err != nil {
return n, err
}
atomic.AddInt64(&r.upSize.Asize, int64(n))
if r.prgNotify != nil {
select {
case r.prgNotify <- r.upSize:
default: //ignore we cannot write
}
}
return n, err
}
func (r *CustomReader) ReadAt(p []byte, off int64) (int, error) {
n, err := r.fp.ReadAt(p, off)
if err != nil {
return n, err
}
// Got the length have read( or means has uploaded), and you can construct your message
atomic.AddInt64(&r.upSize.Asize, int64(n))
if r.prgNotify != nil {
select {
case r.prgNotify <- r.upSize:
default: //ignore we cannot write
}
}
return n, err
}
func (r *CustomReader) Seek(offset int64, whence int) (int64, error) {
return r.fp.Seek(offset, whence)
}
type writerOptions struct {
fp *os.File
upSize types.UpdateStats
name string
prgNotify types.StatsNotifChan
donePartsLock sync.Mutex
err error
}
type CustomWriter struct {
writerGlobalOptions *writerOptions
writtenBytes int64
offset int64
partInd int64
maxPartSize int64
}
func (r *CustomWriter) Write(p []byte) (int, error) {
return r.writerGlobalOptions.fp.Write(p)
}
func (r *CustomWriter) WriteAt(p []byte, off int64) (int, error) {
//adjust offset from begin of the part
off += r.offset
n, err := r.writerGlobalOptions.fp.WriteAt(p, off)
if err != nil {
return n, err
}
r.writtenBytes += int64(n)
r.writerGlobalOptions.donePartsLock.Lock()
// it is quite unexpected but nice to check
if r.writtenBytes > r.maxPartSize {
// invalidating part
r.writerGlobalOptions.upSize.DoneParts.SetPartSize(r.partInd, 0)
r.writerGlobalOptions.donePartsLock.Unlock()
return 0, fmt.Errorf("written (%d) more than expected (%d)", r.writtenBytes, r.maxPartSize)
}
r.writerGlobalOptions.upSize.DoneParts.SetPartSize(r.partInd, r.writtenBytes)
r.writerGlobalOptions.donePartsLock.Unlock()
// Got the length have read (or means has uploaded), and you can construct your message
atomic.AddInt64(&r.writerGlobalOptions.upSize.Asize, int64(n))
if r.writerGlobalOptions.prgNotify != nil {
select {
case r.writerGlobalOptions.prgNotify <- r.writerGlobalOptions.upSize:
default: //ignore we cannot write
}
}
return n, err
}
func (r *CustomWriter) Seek(offset int64, whence int) (int64, error) {
return r.writerGlobalOptions.fp.Seek(offset, whence)
}
func (s *S3ctx) UploadFile(fname, bname, bkey string, compression bool, prgNotify types.StatsNotifChan) (string, error) {
location := ""
// if bucket doesn't exits, create one
ok := s.WaitUntilBucketExists(bname)
if !ok {
err := s.CreateBucket(bname)
if err != nil {
return location, err
}
}
file, err := os.Open(fname)
if err != nil {
return location, err
}
fileInfo, err := file.Stat()
if err != nil {
return location, err
}
creader := &CustomReader{
fp: file,
upSize: types.UpdateStats{Size: fileInfo.Size()},
name: bkey,
prgNotify: prgNotify,
}
reader, writer := io.Pipe()
if compression {
// Note required, but you could zip the file prior to uploading it
// using io.Pipe read/writer to stream gzip'ed file contents.
go func() {
gw := gzip.NewWriter(writer)
_, err := io.Copy(gw, creader)
file.Close()
gw.Close()
_ = writer.CloseWithError(err) //it always returns nil
}()
} else {
go func() {
_, err := io.Copy(writer, creader)
file.Close()
_ = writer.CloseWithError(err) //it always returns nil
}()
}
result, err := s.up.UploadWithContext(s.ctx, &s3manager.UploadInput{
Body: reader, Bucket: aws.String(bname),
Key: aws.String(bkey)})
if err != nil {
return location, err
}
return result.Location, nil
}
// partS3 stores information about part of file in S3 datastore
type partS3 struct {
cWriter *CustomWriter
bname, bkey string
start, size int64 // offset in the file and size of range
}
func (s *S3ctx) downloadPart(ch chan *partS3, wg *sync.WaitGroup) {
defer wg.Done()
for {
p, ok := <-ch
if !ok {
break
}
if p.cWriter.writerGlobalOptions.err != nil {
continue
}
//download range of bytes from file
byteRange := fmt.Sprintf("bytes=%d-%d", p.start, p.start+p.size-1)
_, err := s.dn.DownloadWithContext(s.ctx, p.cWriter, &s3.GetObjectInput{Bucket: aws.String(p.bname),
Key: aws.String(p.bkey),
Range: aws.String(byteRange)})
if err != nil {
p.cWriter.writerGlobalOptions.err = err
}
}
}
//getNeededParts returns description of parts of file to download
//it skips parts from doneParts slice which are fully downloaded
func getNeededParts(cWriterOptions *writerOptions, bname, bkey string, doneParts types.DownloadedParts, size int64) []*partS3 {
partsCount := int64(math.Ceil(float64(size) / float64(S3PartSize)))
var needed []*partS3
for i := int64(0); i < partsCount; i++ {
currentPartSize := int64(0)
for _, val := range doneParts.Parts {
if val.Ind == i {
currentPartSize = val.Size
break
}
}
part := &partS3{
cWriter: &CustomWriter{
writerGlobalOptions: cWriterOptions,
partInd: i,
offset: S3PartSize*i + currentPartSize,
writtenBytes: currentPartSize,
maxPartSize: S3PartSize,
},
bname: bname,
bkey: bkey,
start: S3PartSize*i + currentPartSize,
size: S3PartSize - currentPartSize,
}
if i == partsCount-1 {
part.size = size - (partsCount-1)*S3PartSize - currentPartSize
// adjust maxPartSize for last part to the remaining size
part.cWriter.maxPartSize = size - (partsCount-1)*S3PartSize
}
if part.size > 0 {
needed = append(needed, part)
}
}
return needed
}
func (s *S3ctx) DownloadFile(fname, bname, bkey string,
objMaxSize int64, doneParts types.DownloadedParts, prgNotify types.StatsNotifChan) (types.DownloadedParts, error) {
var fd *os.File
var wg sync.WaitGroup
err, bsize := s.GetObjectSize(bname, bkey)
if err != nil {
return doneParts, err
}
if objMaxSize != 0 && bsize > objMaxSize {
return types.DownloadedParts{PartSize: S3PartSize},
fmt.Errorf("configured image size (%d) is less than size of file (%d)", objMaxSize, bsize)
}
if err := os.MkdirAll(filepath.Dir(fname), 0775); err != nil {
return doneParts, err
}
if _, err := os.Stat(fname); err != nil && os.IsNotExist(err) {
//if file not exists clean doneParts
doneParts = types.DownloadedParts{
PartSize: S3PartSize,
}
}
// if PartSize differ from saved clean doneParts
// we may hit this in case of S3PartSize change or from different type of datastore
if doneParts.PartSize != S3PartSize {
// log only if parts provided
if len(doneParts.Parts) > 0 {
if s.log != nil {
s.log.Warnf("DownloadFile: stored PartSize (%d) is different from expected (%d), assume DownloadedParts are broken",
doneParts.PartSize, S3PartSize)
}
}
doneParts = types.DownloadedParts{
PartSize: S3PartSize,
}
}
if len(doneParts.Parts) > 0 {
fd, err = os.OpenFile(fname, os.O_RDWR, 0666)
if err != nil {
return doneParts, err
}
} else {
// Create the local file
fd, err = os.Create(fname)
if err != nil {
return doneParts, err
}
}
defer fd.Close()
asize := int64(0)
for _, p := range doneParts.Parts {
asize += p.Size
}
cWriterOpts := &writerOptions{
fp: fd,
upSize: types.UpdateStats{Size: bsize, Asize: asize, DoneParts: doneParts},
name: bkey,
prgNotify: prgNotify,
}
ch := make(chan *partS3, S3Concurrency)
neededPart := getNeededParts(cWriterOpts, bname, bkey, doneParts, bsize)
var retryParts []string
for _, el := range neededPart {
if el.cWriter.writtenBytes > 0 {
retryParts = append(retryParts, strconv.FormatInt(el.cWriter.partInd, 10))
}
}
if len(retryParts) > 0 {
if s.log != nil {
s.log.Infof("DownloadFile: Will continue download parts: %s", strings.Join(retryParts, ","))
}
}
//create goroutines to download parts in parallel
for c := 0; c < S3Concurrency; c++ {
wg.Add(1)
go s.downloadPart(ch, &wg)
}
for _, el := range neededPart {
if cWriterOpts.err != nil {
break
}
ch <- el
}
close(ch)
wg.Wait()
return cWriterOpts.upSize.DoneParts, cWriterOpts.err
}
// DownloadFileByChunks downloads the file from s3 chunk by chunk and passes it to the caller
func (s *S3ctx) DownloadFileByChunks(fname, bname, bkey string) (io.ReadCloser, int64, error) {
err, bsize := s.GetObjectSize(bname, bkey)
if err != nil {
return nil, 0, err
}
fmt.Println("size,", bsize)
req, err := s.ss3.GetObjectWithContext(s.ctx, &s3.GetObjectInput{Bucket: aws.String(bname),
Key: aws.String(bkey)})
if err != nil {
return nil, 0, err
}
return req.Body, bsize, nil
}
func (s *S3ctx) ListImages(bname string, prgNotify types.StatsNotifChan) ([]string, error) {
var img []string
input := &s3.ListObjectsInput{
Bucket: aws.String(bname),
}
result, err := s.ss3.ListObjectsWithContext(s.ctx, input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
return img, aerr
default:
return img, aerr
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return img, aerr
}
}
for _, list := range result.Contents {
img = append(img, *list.Key)
}
stats := types.UpdateStats{}
types.SendStats(prgNotify, stats)
return img, nil
}
func (s *S3ctx) GetObjectMetaData(bname, bkey string) (int64, string, error) {
err, bsize := s.GetObjectSize(bname, bkey)
if err != nil {
return 0, "", err
}
err, md5 := s.GetObjectMD5(bname, bkey)
if err != nil {
return 0, "", err
}
return bsize, md5, nil
}
// UploadPart is used to upload the given chunk of data into the Multipart file
func (s *S3ctx) UploadPart(bname, bkey string, chunk []byte, partNumber int64, uploadID string) (string, string, error) {
// initializing Multipart request before uploading parts
if uploadID == "" {
fileType := http.DetectContentType(chunk)
input := &s3.CreateMultipartUploadInput{
Bucket: aws.String(bname),
Key: aws.String(bkey),
ContentType: aws.String(fileType),
}
multiPartUplladCreateResponse, err := s.ss3.CreateMultipartUpload(input)
if err != nil {
return "", "", err
}
uploadID = *multiPartUplladCreateResponse.UploadId
}
partInput := &s3.UploadPartInput{
Body: bytes.NewReader(chunk),
Bucket: &bname,
Key: &bkey,
PartNumber: aws.Int64(partNumber),
UploadId: &uploadID,
ContentLength: aws.Int64(int64(len(chunk))),
}
uploadResult, err := s.ss3.UploadPart(partInput)
if err != nil {
return "", "", err
}
return *uploadResult.ETag, uploadID, err
}
// CompleteUploadedParts is used to complete the multiple uploaded parts
func (s *S3ctx) CompleteUploadedParts(bname, bkey, uploadID string, parts []string) error {
completeInput := &s3.CompleteMultipartUploadInput{
Bucket: &bname,
Key: &bkey,
UploadId: &uploadID,
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: getUploadedParts(parts),
},
}
_, err := s.ss3.CompleteMultipartUpload(completeInput)
if err != nil {
return err
}
return nil
}
// GetSignedURL is used to generate the URI which can be used to access the resource until the URI expries
func (s *S3ctx) GetSignedURL(bname, bkey string, duration time.Duration) (string, error) {
_, err := s.ss3.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(bname),
Key: aws.String(bkey)})
if err != nil {
return "", err
}
req, _ := s.ss3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bname),
Key: aws.String(bkey)})
// Presign a request with specified duration.
signedURL, err := req.Presign(duration)
return signedURL, err
}
func getUploadedParts(parts []string) []*s3.CompletedPart {
var completedParts []*s3.CompletedPart
for i := 0; i < len(parts); i++ {
part := s3.CompletedPart{
ETag: &parts[i],
PartNumber: aws.Int64(int64(i + 1))}
completedParts = append(completedParts, &part)
}
return completedParts
}