-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy paths3sync.go
635 lines (560 loc) · 15.5 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright 2019 SEQSENSE, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package s3sync
import (
"context"
"errors"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gabriel-vasile/mimetype"
)
// Manager manages the sync operation.
type Manager struct {
s3 s3iface.S3API
nJobs int
del bool
dryrun bool
acl *string
guessMime bool
contentType *string
downloaderOpts []func(*s3manager.Downloader)
uploaderOpts []func(*s3manager.Uploader)
statistics SyncStatistics
}
// SyncStatistics captures the sync statistics.
type SyncStatistics struct {
Bytes int64
Files int64
DeletedFiles int64
mutex sync.RWMutex
}
type operation int
const (
opUpdate operation = iota
opDelete
)
type fileInfo struct {
name string
err error
path string
size int64
lastModified time.Time
singleFile bool
existsInSource bool
}
type fileOp struct {
*fileInfo
op operation
}
// New returns a new Manager.
func New(sess *session.Session, options ...Option) *Manager {
m := &Manager{
s3: s3.New(sess),
nJobs: DefaultParallel,
guessMime: true,
}
for _, o := range options {
o(m)
}
return m
}
// Sync syncs the files between s3 and local disks.
func (m *Manager) Sync(source, dest string) error {
return m.SyncWithContext(context.Background(), source, dest)
}
// SyncWithContext syncs the files between s3 and local disks.
// The context will be used for operation cancellation.
func (m *Manager) SyncWithContext(ctx context.Context, source, dest string) error {
sourceURL, err := url.Parse(source)
if err != nil {
return err
}
destURL, err := url.Parse(dest)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
chJob := make(chan func())
var wg sync.WaitGroup
for i := 0; i < m.nJobs; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range chJob {
job()
}
}()
}
defer func() {
close(chJob)
wg.Wait()
}()
if isS3URL(sourceURL) {
sourceS3Path, err := urlToS3Path(sourceURL)
if err != nil {
return err
}
if isS3URL(destURL) {
destS3Path, err := urlToS3Path(destURL)
if err != nil {
return err
}
return m.syncS3ToS3(ctx, chJob, sourceS3Path, destS3Path)
}
return m.syncS3ToLocal(ctx, chJob, sourceS3Path, dest)
}
if isS3URL(destURL) {
destS3Path, err := urlToS3Path(destURL)
if err != nil {
return err
}
return m.syncLocalToS3(ctx, chJob, source, destS3Path)
}
return errors.New("local to local sync is not supported")
}
// GetStatistics returns the structure that contains the sync statistics
func (m *Manager) GetStatistics() SyncStatistics {
m.statistics.mutex.Lock()
defer m.statistics.mutex.Unlock()
return SyncStatistics{Bytes: m.statistics.Bytes, Files: m.statistics.Files, DeletedFiles: m.statistics.DeletedFiles}
}
func isS3URL(url *url.URL) bool {
return url.Scheme == "s3"
}
func (m *Manager) syncS3ToS3(ctx context.Context, chJob chan func(), sourcePath *s3Path, destPath *s3Path) error {
wg := &sync.WaitGroup{}
errs := &multiErr{}
for source := range filterFilesForSync(
m.listS3Files(ctx, sourcePath), m.listS3Files(ctx, destPath), m.del,
) {
wg.Add(1)
source := source
chJob <- func() {
defer wg.Done()
if source.err != nil {
errs.Append(source.err)
return
}
switch source.op {
case opUpdate:
if err := m.copyS3ToS3(ctx, source.fileInfo, sourcePath, destPath); err != nil {
errs.Append(err)
}
}
}
}
wg.Wait()
return errs.ErrOrNil()
}
func (m *Manager) syncLocalToS3(ctx context.Context, chJob chan func(), sourcePath string, destPath *s3Path) error {
wg := &sync.WaitGroup{}
errs := &multiErr{}
for source := range filterFilesForSync(
listLocalFiles(ctx, sourcePath), m.listS3Files(ctx, destPath), m.del,
) {
wg.Add(1)
source := source
chJob <- func() {
defer wg.Done()
if source.err != nil {
errs.Append(source.err)
return
}
switch source.op {
case opUpdate:
if err := m.upload(source.fileInfo, sourcePath, destPath); err != nil {
errs.Append(err)
}
case opDelete:
if err := m.deleteRemote(source.fileInfo, destPath); err != nil {
errs.Append(err)
}
}
}
}
wg.Wait()
return errs.ErrOrNil()
}
// syncS3ToLocal syncs the given s3 path to the given local path.
func (m *Manager) syncS3ToLocal(ctx context.Context, chJob chan func(), sourcePath *s3Path, destPath string) error {
wg := &sync.WaitGroup{}
errs := &multiErr{}
for source := range filterFilesForSync(
m.listS3Files(ctx, sourcePath), listLocalFiles(ctx, destPath), m.del,
) {
wg.Add(1)
source := source
chJob <- func() {
defer wg.Done()
if source.err != nil {
errs.Append(source.err)
return
}
switch source.op {
case opUpdate:
if err := m.download(source.fileInfo, sourcePath, destPath); err != nil {
errs.Append(err)
}
case opDelete:
if err := m.deleteLocal(source.fileInfo, destPath); err != nil {
errs.Append(err)
}
}
}
}
wg.Wait()
return errs.ErrOrNil()
}
func (m *Manager) copyS3ToS3(ctx context.Context, file *fileInfo, sourcePath *s3Path, destPath *s3Path) error {
copySource := filepath.ToSlash(filepath.Join(sourcePath.bucket, sourcePath.bucketPrefix, file.name))
destinationKey := filepath.ToSlash(filepath.Join(destPath.bucketPrefix, file.name))
println("Copying from", copySource, "to key", destinationKey, "in bucket", destPath.bucket)
if m.dryrun {
return nil
}
_, err := m.s3.CopyObject(&s3.CopyObjectInput{
Bucket: aws.String(destPath.bucket),
CopySource: aws.String(copySource),
Key: aws.String(destinationKey),
ACL: m.acl,
})
if err != nil {
return err
}
m.updateFileTransferStatistics(file.size)
return nil
}
func (m *Manager) download(file *fileInfo, sourcePath *s3Path, destPath string) error {
var targetFilename string
if !strings.HasSuffix(destPath, "/") && file.singleFile {
// Destination path is not a directory and source is a single file.
targetFilename = destPath
} else {
targetFilename = filepath.Join(destPath, file.name)
}
targetDir := filepath.Dir(targetFilename)
println("Downloading", file.name, "to", targetFilename)
if m.dryrun {
return nil
}
if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}
writer, err := os.Create(targetFilename)
if err != nil {
return err
}
defer writer.Close()
var sourceFile string
if file.singleFile {
sourceFile = file.name
} else {
// Using filepath.ToSlash for change backslash to slash on Windows
sourceFile = filepath.ToSlash(filepath.Join(sourcePath.bucketPrefix, file.name))
}
c := s3manager.NewDownloaderWithClient(m.s3, m.downloaderOpts...)
written, err := c.Download(writer, &s3.GetObjectInput{
Bucket: aws.String(sourcePath.bucket),
Key: aws.String(sourceFile),
})
if err != nil {
return err
}
m.updateFileTransferStatistics(written)
err = os.Chtimes(targetFilename, file.lastModified, file.lastModified)
if err != nil {
return err
}
return nil
}
func (m *Manager) deleteLocal(file *fileInfo, destPath string) error {
var targetFilename string
if !strings.HasSuffix(destPath, "/") && file.singleFile {
// Destination path is not a directory and source is a single file.
targetFilename = destPath
} else {
targetFilename = filepath.Join(destPath, file.name)
}
println("Deleting", targetFilename)
if m.dryrun {
return nil
}
err := os.Remove(targetFilename)
if err != nil {
return err
}
m.incrementDeletedFiles()
return nil
}
func (m *Manager) upload(file *fileInfo, sourcePath string, destPath *s3Path) error {
var sourceFilename string
if file.singleFile {
sourceFilename = sourcePath
} else {
sourceFilename = filepath.Join(sourcePath, file.name)
}
destFile := *destPath
if strings.HasSuffix(destPath.bucketPrefix, "/") || destPath.bucketPrefix == "" || !file.singleFile {
// If source is a single file and destination is not a directory, use destination URL as is.
// Using filepath.ToSlash for change backslash to slash on Windows
destFile.bucketPrefix = filepath.ToSlash(filepath.Join(destPath.bucketPrefix, file.name))
}
println("Uploading", file.name, "to", destFile.String())
if m.dryrun {
return nil
}
var contentType *string
switch {
case m.contentType != nil:
contentType = m.contentType
case m.guessMime:
mime, err := mimetype.DetectFile(sourceFilename)
if err != nil {
return err
}
s := mime.String()
contentType = &s
}
reader, err := os.Open(sourceFilename)
if err != nil {
return err
}
defer reader.Close()
_, err = s3manager.NewUploaderWithClient(
m.s3,
m.uploaderOpts...,
).Upload(&s3manager.UploadInput{
Bucket: aws.String(destFile.bucket),
Key: aws.String(destFile.bucketPrefix),
ACL: m.acl,
Body: reader,
ContentType: contentType,
})
if err != nil {
return err
}
m.updateFileTransferStatistics(file.size)
return nil
}
func (m *Manager) deleteRemote(file *fileInfo, destPath *s3Path) error {
destFile := *destPath
if strings.HasSuffix(destPath.bucketPrefix, "/") || destPath.bucketPrefix == "" || !file.singleFile {
// If source is a single file and destination is not a directory, use destination URL as is.
// Using filepath.ToSlash for change backslash to slash on Windows
destFile.bucketPrefix = filepath.ToSlash(filepath.Join(destPath.bucketPrefix, file.name))
}
println("Deleting", destFile.String())
if m.dryrun {
return nil
}
_, err := m.s3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(destFile.bucket),
Key: aws.String(destFile.bucketPrefix),
})
if err != nil {
return err
}
m.incrementDeletedFiles()
return nil
}
// listS3Files return a channel which receives the file infos under the given s3Path.
func (m *Manager) listS3Files(ctx context.Context, path *s3Path) chan *fileInfo {
c := make(chan *fileInfo, 50000) // TODO: revisit this buffer size later
go func() {
defer close(c)
var token *string
for {
if token = m.listS3FileWithToken(ctx, c, path, token); token == nil {
break
}
}
}()
return c
}
// listS3FileWithToken lists (send to the result channel) the s3 files from the given continuation token.
func (m *Manager) listS3FileWithToken(ctx context.Context, c chan *fileInfo, path *s3Path, token *string) *string {
list, err := m.s3.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: &path.bucket,
Prefix: &path.bucketPrefix,
ContinuationToken: token,
})
if err != nil {
sendErrorInfoToChannel(ctx, c, err)
return nil
}
for _, object := range list.Contents {
if strings.HasSuffix(*object.Key, "/") {
// Skip directory like object
continue
}
name, err := filepath.Rel(path.bucketPrefix, *object.Key)
if err != nil {
sendErrorInfoToChannel(ctx, c, err)
continue
}
var fi *fileInfo
if name == "." {
// Single file was specified
fi = &fileInfo{
name: filepath.Base(*object.Key),
path: filepath.Dir(*object.Key),
size: *object.Size,
lastModified: *object.LastModified,
singleFile: true,
}
} else {
fi = &fileInfo{
name: name,
path: *object.Key,
size: *object.Size,
lastModified: *object.LastModified,
}
}
select {
case c <- fi:
case <-ctx.Done():
return nil
}
}
return list.NextContinuationToken
}
// updateSyncStatistics updates the statistics of the amount of bytes transferred for one file
func (m *Manager) updateFileTransferStatistics(written int64) {
m.statistics.mutex.Lock()
defer m.statistics.mutex.Unlock()
m.statistics.Files++
m.statistics.Bytes += written
}
// incrementDeletedFiles increments the counter used to capture the number of remote files deleted during the synchronization process
func (m *Manager) incrementDeletedFiles() {
m.statistics.mutex.Lock()
defer m.statistics.mutex.Unlock()
m.statistics.DeletedFiles++
}
// listLocalFiles returns a channel which receives the infos of the files under the given basePath.
// basePath have to be absolute path.
func listLocalFiles(ctx context.Context, basePath string) chan *fileInfo {
c := make(chan *fileInfo)
basePath = filepath.ToSlash(basePath)
go func() {
defer close(c)
stat, err := os.Stat(basePath)
if os.IsNotExist(err) {
// The path doesn't exist.
// Returns and closes the channel without sending any.
return
} else if err != nil {
sendErrorInfoToChannel(ctx, c, err)
return
}
if !stat.IsDir() {
sendFileInfoToChannel(ctx, c, filepath.Dir(basePath), basePath, stat, true)
return
}
sendFileInfoToChannel(ctx, c, basePath, basePath, stat, false)
err = filepath.Walk(basePath, func(path string, stat os.FileInfo, err error) error {
if err != nil {
return err
}
sendFileInfoToChannel(ctx, c, basePath, path, stat, false)
return ctx.Err()
})
if err != nil {
sendErrorInfoToChannel(ctx, c, err)
}
}()
return c
}
func sendFileInfoToChannel(ctx context.Context, c chan *fileInfo, basePath, path string, stat os.FileInfo, singleFile bool) {
if stat == nil || stat.IsDir() {
return
}
relPath, _ := filepath.Rel(basePath, path)
fi := &fileInfo{
name: relPath,
path: path,
size: stat.Size(),
lastModified: stat.ModTime(),
singleFile: singleFile,
}
select {
case c <- fi:
case <-ctx.Done():
}
}
func sendErrorInfoToChannel(ctx context.Context, c chan *fileInfo, err error) {
fi := &fileInfo{
err: err,
}
select {
case c <- fi:
case <-ctx.Done():
}
}
// filterFilesForSync filters the source files from the given destination files, and returns
// another channel which includes the files necessary to be synced.
func filterFilesForSync(sourceFileChan, destFileChan chan *fileInfo, del bool) chan *fileOp {
c := make(chan *fileOp)
destFiles, err := fileInfoChanToMap(destFileChan)
go func() {
defer close(c)
if err != nil {
c <- &fileOp{fileInfo: &fileInfo{err: err}}
return
}
for sourceInfo := range sourceFileChan {
destInfo, ok := destFiles[sourceInfo.name]
// source is necessary to sync if
// 1. The dest doesn't exist
// 2. The dest doesn't have the same size as the source
// 3. The dest is older than the source
if !ok || sourceInfo.size != destInfo.size || sourceInfo.lastModified.After(destInfo.lastModified) {
c <- &fileOp{fileInfo: sourceInfo}
}
if ok {
destInfo.existsInSource = true
}
}
if del {
for _, destInfo := range destFiles {
if !destInfo.existsInSource {
// The source doesn't exist
c <- &fileOp{fileInfo: destInfo, op: opDelete}
}
}
}
}()
return c
}
// fileInfoChanToMap accumulates the fileInfos from the given channel and returns a map.
// It retruns an error if the channel contains an error.
func fileInfoChanToMap(files chan *fileInfo) (map[string]*fileInfo, error) {
result := make(map[string]*fileInfo)
for file := range files {
if file.err != nil {
return nil, file.err
}
result[file.name] = file
}
return result, nil
}