-
Notifications
You must be signed in to change notification settings - Fork 211
/
s3.go
751 lines (691 loc) · 23.2 KB
/
s3.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package storage
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"path"
"sort"
"strings"
"sync"
"time"
"github.com/Altinity/clickhouse-backup/v2/pkg/config"
apexLog "github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
awsV2Config "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go"
awsV2Logging "github.com/aws/smithy-go/logging"
awsV2http "github.com/aws/smithy-go/transport/http"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
type S3LogToApexLogAdapter struct {
apexLog *apexLog.Logger
}
func newS3Logger(log *apexLog.Entry) S3LogToApexLogAdapter {
return S3LogToApexLogAdapter{
apexLog: log.Logger,
}
}
func (S3LogToApexLogAdapter S3LogToApexLogAdapter) Logf(severity awsV2Logging.Classification, msg string, args ...interface{}) {
msg = fmt.Sprintf("[s3:%s] %s", severity, msg)
if len(args) > 0 {
S3LogToApexLogAdapter.apexLog.Infof(msg, args...)
} else {
S3LogToApexLogAdapter.apexLog.Info(msg)
}
}
// RecalculateV4Signature allow GCS over S3, remove Accept-Encoding header from sign https://stackoverflow.com/a/74382598/1204665, https://github.com/aws/aws-sdk-go-v2/issues/1816
type RecalculateV4Signature struct {
next http.RoundTripper
signer *v4.Signer
awsConfig aws.Config
}
func (lt *RecalculateV4Signature) RoundTrip(req *http.Request) (*http.Response, error) {
// store for later use
acceptEncodingValue := req.Header.Get("Accept-Encoding")
// delete the header so the header doesn't account for in the signature
req.Header.Del("Accept-Encoding")
// sign with the same date
timeString := req.Header.Get("X-Amz-Date")
timeDate, _ := time.Parse("20060102T150405Z", timeString)
creds, err := lt.awsConfig.Credentials.Retrieve(req.Context())
if err != nil {
return nil, err
}
err = lt.signer.SignHTTP(req.Context(), creds, req, v4.GetPayloadHash(req.Context()), "s3", lt.awsConfig.Region, timeDate)
if err != nil {
return nil, err
}
// Reset Accept-Encoding if desired
req.Header.Set("Accept-Encoding", acceptEncodingValue)
// follows up the original round tripper
return lt.next.RoundTrip(req)
}
// S3 - presents methods for manipulate data on s3
type S3 struct {
client *s3.Client
uploader *s3manager.Uploader
downloader *s3manager.Downloader
Config *config.S3Config
Log *apexLog.Entry
PartSize int64
Concurrency int
BufferSize int
versioning bool
}
func (s *S3) Kind() string {
return "S3"
}
// Connect - connect to s3
func (s *S3) Connect(ctx context.Context) error {
var err error
var awsConfig aws.Config
awsConfig, err = awsV2Config.LoadDefaultConfig(
ctx,
awsV2Config.WithRetryMode(aws.RetryModeAdaptive),
)
if err != nil {
return err
}
if s.Config.Region != "" {
awsConfig.Region = s.Config.Region
}
// AWS IRSA handling, look https://github.com/Altinity/clickhouse-backup/issues/798
awsRoleARN := os.Getenv("AWS_ROLE_ARN")
awsWebIdentityTokenFile := os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE")
stsClient := sts.NewFromConfig(awsConfig)
if awsRoleARN != "" && awsWebIdentityTokenFile != "" {
awsConfig.Credentials = stscreds.NewWebIdentityRoleProvider(
stsClient, awsRoleARN, stscreds.IdentityTokenFile(awsWebIdentityTokenFile),
)
} else if s.Config.AssumeRoleARN != "" {
// backup role S3_ASSUME_ROLE_ARN have high priority than AWS_ROLE_ARN see https://github.com/Altinity/clickhouse-backup/issues/898
awsConfig.Credentials = stscreds.NewAssumeRoleProvider(stsClient, s.Config.AssumeRoleARN)
} else if awsRoleARN != "" {
awsConfig.Credentials = stscreds.NewAssumeRoleProvider(stsClient, awsRoleARN)
}
if s.Config.AccessKey != "" && s.Config.SecretKey != "" {
awsConfig.Credentials = credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: s.Config.AccessKey,
SecretAccessKey: s.Config.SecretKey,
},
}
}
if s.Config.Debug {
awsConfig.Logger = newS3Logger(s.Log)
awsConfig.ClientLogMode = aws.LogRetries | aws.LogRequest | aws.LogResponse
}
httpTransport := http.DefaultTransport
if s.Config.DisableCertVerification {
httpTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
awsConfig.HTTPClient = &http.Client{Transport: httpTransport}
}
if s.Config.Endpoint != "" {
awsConfig.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: s.Config.Endpoint,
SigningRegion: s.Config.Region,
HostnameImmutable: true,
Source: aws.EndpointSourceCustom,
}, nil
})
}
// allow GCS over S3, remove Accept-Encoding header from sign https://stackoverflow.com/a/74382598/1204665, https://github.com/aws/aws-sdk-go-v2/issues/1816
if strings.Contains(s.Config.Endpoint, "storage.googleapis.com") {
// Assign custom client with our own transport
awsConfig.HTTPClient = &http.Client{Transport: &RecalculateV4Signature{httpTransport, v4.NewSigner(func(signer *v4.SignerOptions) {
signer.DisableURIPathEscaping = true
}), awsConfig}}
}
s.client = s3.NewFromConfig(awsConfig, func(o *s3.Options) {
o.UsePathStyle = s.Config.ForcePathStyle
o.EndpointOptions.DisableHTTPS = s.Config.DisableSSL
})
s.uploader = s3manager.NewUploader(s.client)
s.uploader.Concurrency = s.Concurrency
s.uploader.BufferProvider = s3manager.NewBufferedReadSeekerWriteToPool(s.BufferSize)
s.uploader.PartSize = s.PartSize
s.downloader = s3manager.NewDownloader(s.client)
s.downloader.Concurrency = s.Concurrency
s.downloader.BufferProvider = s3manager.NewPooledBufferedWriterReadFromProvider(s.BufferSize)
s.downloader.PartSize = s.PartSize
s.versioning = s.isVersioningEnabled(ctx)
return nil
}
func (s *S3) Close(ctx context.Context) error {
return nil
}
func (s *S3) GetFileReader(ctx context.Context, key string) (io.ReadCloser, error) {
return s.GetFileReaderAbsolute(ctx, path.Join(s.Config.Path, key))
}
func (s *S3) GetFileReaderAbsolute(ctx context.Context, key string) (io.ReadCloser, error) {
params := &s3.GetObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(key),
}
s.enrichGetObjectParams(params)
resp, err := s.client.GetObject(ctx, params)
if err != nil {
var opError *smithy.OperationError
if errors.As(err, &opError) {
var httpErr *awsV2http.ResponseError
if errors.As(opError.Err, &httpErr) {
var stateErr *s3types.InvalidObjectState
if errors.As(httpErr, &stateErr) {
if strings.Contains(string(stateErr.StorageClass), "GLACIER") {
s.Log.Warnf("GetFileReader %s, storageClass %s receive error: %s", key, stateErr.StorageClass, stateErr.Error())
if restoreErr := s.restoreObject(ctx, key); restoreErr != nil {
s.Log.Warnf("restoreObject %s, return error: %v", key, restoreErr)
return nil, err
}
if resp, err = s.client.GetObject(ctx, params); err != nil {
s.Log.Warnf("second GetObject %s, return error: %v", key, err)
return nil, err
}
return resp.Body, nil
}
}
}
return nil, err
}
return nil, err
}
return resp.Body, nil
}
func (s *S3) enrichGetObjectParams(params *s3.GetObjectInput) {
if s.Config.SSECustomerAlgorithm != "" {
params.SSECustomerAlgorithm = aws.String(s.Config.SSECustomerAlgorithm)
}
if s.Config.SSECustomerKey != "" {
params.SSECustomerKey = aws.String(s.Config.SSECustomerKey)
}
if s.Config.SSECustomerKeyMD5 != "" {
params.SSECustomerKeyMD5 = aws.String(s.Config.SSECustomerKeyMD5)
}
if s.Config.RequestPayer != "" {
params.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
}
func (s *S3) GetFileReaderWithLocalPath(ctx context.Context, key, localPath string) (io.ReadCloser, error) {
/* unfortunately, multipart download require allocate additional disk space
and don't allow us to decompress data directly from stream */
if s.Config.AllowMultipartDownload {
writer, err := os.CreateTemp(localPath, strings.ReplaceAll(key, "/", "_"))
if err != nil {
return nil, err
}
_, err = s.downloader.Download(ctx, writer, &s3.GetObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(path.Join(s.Config.Path, key)),
})
if err != nil {
return nil, err
}
return writer, nil
} else {
return s.GetFileReader(ctx, key)
}
}
func (s *S3) PutFile(ctx context.Context, key string, r io.ReadCloser) error {
return s.PutFileAbsolute(ctx, path.Join(s.Config.Path, key), r)
}
func (s *S3) PutFileAbsolute(ctx context.Context, key string, r io.ReadCloser) error {
params := s3.PutObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(key),
Body: r,
StorageClass: s3types.StorageClass(strings.ToUpper(s.Config.StorageClass)),
}
if s.Config.CheckSumAlgorithm != "" {
params.ChecksumAlgorithm = s3types.ChecksumAlgorithm(s.Config.CheckSumAlgorithm)
}
// ACL shall be optional, fix https://github.com/Altinity/clickhouse-backup/issues/785
if s.Config.ACL != "" {
params.ACL = s3types.ObjectCannedACL(s.Config.ACL)
}
// https://github.com/Altinity/clickhouse-backup/issues/588
if len(s.Config.ObjectLabels) > 0 {
tags := ""
for k, v := range s.Config.ObjectLabels {
if tags != "" {
tags += "&"
}
tags += k + "=" + v
}
params.Tagging = aws.String(tags)
}
if s.Config.SSE != "" {
params.ServerSideEncryption = s3types.ServerSideEncryption(s.Config.SSE)
}
if s.Config.SSEKMSKeyId != "" {
params.SSEKMSKeyId = aws.String(s.Config.SSEKMSKeyId)
}
if s.Config.SSECustomerAlgorithm != "" {
params.SSECustomerAlgorithm = aws.String(s.Config.SSECustomerAlgorithm)
}
if s.Config.SSECustomerKey != "" {
params.SSECustomerKey = aws.String(s.Config.SSECustomerKey)
}
if s.Config.SSECustomerKeyMD5 != "" {
params.SSECustomerKeyMD5 = aws.String(s.Config.SSECustomerKeyMD5)
}
if s.Config.SSEKMSEncryptionContext != "" {
params.SSEKMSEncryptionContext = aws.String(s.Config.SSEKMSEncryptionContext)
}
_, err := s.uploader.Upload(ctx, ¶ms)
return err
}
func (s *S3) deleteKey(ctx context.Context, key string) error {
params := &s3.DeleteObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(key),
}
if s.Config.RequestPayer != "" {
params.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
if s.versioning {
objVersion, err := s.getObjectVersion(ctx, key)
if err != nil {
return errors.Wrapf(err, "deleteKey, obtaining object version bucket: %s key: %s", s.Config.Bucket, key)
}
params.VersionId = objVersion
}
if _, err := s.client.DeleteObject(ctx, params); err != nil {
return errors.Wrapf(err, "deleteKey, deleting object bucket: %s key: %s version: %v", s.Config.Bucket, key, params.VersionId)
}
return nil
}
func (s *S3) DeleteFile(ctx context.Context, key string) error {
key = path.Join(s.Config.Path, key)
return s.deleteKey(ctx, key)
}
func (s *S3) DeleteFileFromObjectDiskBackup(ctx context.Context, key string) error {
key = path.Join(s.Config.ObjectDiskPath, key)
return s.deleteKey(ctx, key)
}
func (s *S3) isVersioningEnabled(ctx context.Context) bool {
output, err := s.client.GetBucketVersioning(ctx, &s3.GetBucketVersioningInput{
Bucket: aws.String(s.Config.Bucket),
})
if err != nil {
return false
}
return output.Status == s3types.BucketVersioningStatusEnabled
}
func (s *S3) getObjectVersion(ctx context.Context, key string) (*string, error) {
params := &s3.HeadObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(key),
}
if s.Config.RequestPayer != "" {
params.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
object, err := s.client.HeadObject(ctx, params)
if err != nil {
return nil, err
}
return object.VersionId, nil
}
func (s *S3) StatFile(ctx context.Context, key string) (RemoteFile, error) {
params := &s3.HeadObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(path.Join(s.Config.Path, key)),
}
s.enrichHeadParams(params)
head, err := s.client.HeadObject(ctx, params)
if err != nil {
var opError *smithy.OperationError
if errors.As(err, &opError) {
var httpErr *awsV2http.ResponseError
if errors.As(opError.Err, &httpErr) {
if httpErr.Response.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
}
}
}
return nil, err
}
return &s3File{*head.ContentLength, *head.LastModified, string(head.StorageClass), key}, nil
}
func (s *S3) Walk(ctx context.Context, s3Path string, recursive bool, process func(ctx context.Context, r RemoteFile) error) error {
prefix := path.Join(s.Config.Path, s3Path)
return s.WalkAbsolute(ctx, prefix, recursive, process)
}
func (s *S3) WalkAbsolute(ctx context.Context, prefix string, recursive bool, process func(ctx context.Context, r RemoteFile) error) error {
g, ctx := errgroup.WithContext(ctx)
s3Files := make(chan *s3File)
g.Go(func() error {
defer close(s3Files)
return s.remotePager(ctx, prefix, recursive, func(page *s3.ListObjectsV2Output) {
for _, cp := range page.CommonPrefixes {
s3Files <- &s3File{
name: strings.TrimPrefix(*cp.Prefix, prefix),
}
}
for _, c := range page.Contents {
s3Files <- &s3File{
*c.Size,
*c.LastModified,
string(c.StorageClass),
strings.TrimPrefix(*c.Key, prefix),
}
}
})
})
g.Go(func() error {
var err error
for s3FileItem := range s3Files {
if err == nil {
err = process(ctx, s3FileItem)
}
}
return err
})
return g.Wait()
}
func (s *S3) remotePager(ctx context.Context, s3Path string, recursive bool, process func(page *s3.ListObjectsV2Output)) error {
prefix := s3Path + "/"
if s3Path == "" || s3Path == "/" {
prefix = ""
}
params := &s3.ListObjectsV2Input{
Bucket: aws.String(s.Config.Bucket), // Required
MaxKeys: aws.Int32(1000),
Prefix: aws.String(prefix),
}
if !recursive {
params.Delimiter = aws.String("/")
}
pager := s3.NewListObjectsV2Paginator(s.client, params, func(o *s3.ListObjectsV2PaginatorOptions) {
o.Limit = 1000
})
for pager.HasMorePages() {
page, err := pager.NextPage(ctx)
if err != nil {
return err
}
process(page)
}
return nil
}
func (s *S3) CopyObject(ctx context.Context, srcSize int64, srcBucket, srcKey, dstKey string) (int64, error) {
dstKey = path.Join(s.Config.ObjectDiskPath, dstKey)
s.Log.Debugf("S3->CopyObject %s/%s -> %s/%s", srcBucket, srcKey, s.Config.Bucket, dstKey)
// just copy object without multipart
if srcSize < 5*1024*1024*1024 || strings.Contains(s.Config.Endpoint, "storage.googleapis.com") {
params := &s3.CopyObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(dstKey),
CopySource: aws.String(path.Join(srcBucket, srcKey)),
StorageClass: s3types.StorageClass(strings.ToUpper(s.Config.StorageClass)),
}
s.enrichCopyObjectParams(params)
_, err := s.client.CopyObject(ctx, params)
if err != nil {
return 0, err
}
return srcSize, nil
}
// Initiate a multipart upload
createMultipartUploadParams := &s3.CreateMultipartUploadInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(dstKey),
StorageClass: s3types.StorageClass(strings.ToUpper(s.Config.StorageClass)),
}
s.enrichCreateMultipartUploadParams(createMultipartUploadParams)
initResp, err := s.client.CreateMultipartUpload(ctx, createMultipartUploadParams)
if err != nil {
return 0, err
}
// Get the upload ID
uploadID := initResp.UploadId
// Set the part size (e.g., 5 MB)
partSize := srcSize / s.Config.MaxPartsCount
if srcSize%s.Config.MaxPartsCount > 0 {
partSize++
}
// 128Mb part size recommendation from https://repost.aws/questions/QUtW2_XaALTK63wv9XLSywiQ/s3-sync-command-is-slow-to-start-on-some-data
if partSize < 128*1024*1024 {
partSize = 128 * 1024 * 1024
}
if partSize > 5*1024*1024*1024 {
partSize = 5 * 1024 * 1024 * 1024
}
// Calculate the number of parts
numParts := (srcSize + partSize - 1) / partSize
copyPartErrGroup, ctx := errgroup.WithContext(ctx)
copyPartErrGroup.SetLimit(s.Config.Concurrency * s.Config.Concurrency)
var mu sync.Mutex
var parts []s3types.CompletedPart
// Copy each part of the object
for partNumber := int64(1); partNumber <= numParts; partNumber++ {
// Calculate the byte range for the part
start := (partNumber - 1) * partSize
end := partNumber * partSize
if end > srcSize {
end = srcSize
}
currentPartNumber := int32(partNumber)
copyPartErrGroup.Go(func() error {
// Copy the part
uploadPartParams := &s3.UploadPartCopyInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(dstKey),
CopySource: aws.String(srcBucket + "/" + srcKey),
CopySourceRange: aws.String(fmt.Sprintf("bytes=%d-%d", start, end-1)),
UploadId: uploadID,
PartNumber: aws.Int32(currentPartNumber),
}
if s.Config.RequestPayer != "" {
uploadPartParams.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
partResp, err := s.client.UploadPartCopy(ctx, uploadPartParams)
if err != nil {
return err
}
mu.Lock()
parts = append(parts, s3types.CompletedPart{
ETag: partResp.CopyPartResult.ETag,
PartNumber: aws.Int32(currentPartNumber),
ChecksumCRC32: partResp.CopyPartResult.ChecksumCRC32,
ChecksumCRC32C: partResp.CopyPartResult.ChecksumCRC32C,
ChecksumSHA1: partResp.CopyPartResult.ChecksumSHA1,
ChecksumSHA256: partResp.CopyPartResult.ChecksumSHA256,
})
mu.Unlock()
return nil
})
}
if wgWaitErr := copyPartErrGroup.Wait(); wgWaitErr != nil {
abortParams := &s3.AbortMultipartUploadInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(dstKey),
UploadId: uploadID,
}
if s.Config.RequestPayer != "" {
abortParams.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
_, abortErr := s.client.AbortMultipartUpload(context.Background(), abortParams)
if abortErr != nil {
return 0, fmt.Errorf("aborting CopyObject multipart upload: %v, original error was: %v", abortErr, wgWaitErr)
}
return 0, fmt.Errorf("one of CopyObject go-routine return error: %v", wgWaitErr)
}
// Parts must be ordered by part number.
sort.Slice(parts, func(i int, j int) bool {
return *parts[i].PartNumber < *parts[j].PartNumber
})
// Complete the multipart upload
completeMultipartUploadParams := &s3.CompleteMultipartUploadInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(dstKey),
UploadId: uploadID,
MultipartUpload: &s3types.CompletedMultipartUpload{Parts: parts},
}
if s.Config.RequestPayer != "" {
completeMultipartUploadParams.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
_, err = s.client.CompleteMultipartUpload(context.Background(), completeMultipartUploadParams)
if err != nil {
return 0, fmt.Errorf("complete CopyObject multipart upload: %v", err)
}
return srcSize, nil
}
func (s *S3) enrichCreateMultipartUploadParams(params *s3.CreateMultipartUploadInput) {
if s.Config.CheckSumAlgorithm != "" {
params.ChecksumAlgorithm = s3types.ChecksumAlgorithm(s.Config.CheckSumAlgorithm)
}
if s.Config.RequestPayer != "" {
params.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
// https://github.com/Altinity/clickhouse-backup/issues/588
if len(s.Config.ObjectLabels) > 0 {
tags := ""
for k, v := range s.Config.ObjectLabels {
if tags != "" {
tags += "&"
}
tags += k + "=" + v
}
params.Tagging = aws.String(tags)
}
if s.Config.SSE != "" {
params.ServerSideEncryption = s3types.ServerSideEncryption(s.Config.SSE)
}
if s.Config.SSEKMSKeyId != "" {
params.SSEKMSKeyId = aws.String(s.Config.SSEKMSKeyId)
}
if s.Config.SSECustomerAlgorithm != "" {
params.SSECustomerAlgorithm = aws.String(s.Config.SSECustomerAlgorithm)
}
if s.Config.SSECustomerKey != "" {
params.SSECustomerKey = aws.String(s.Config.SSECustomerKey)
}
if s.Config.SSECustomerKeyMD5 != "" {
params.SSECustomerKeyMD5 = aws.String(s.Config.SSECustomerKeyMD5)
}
if s.Config.SSEKMSEncryptionContext != "" {
params.SSEKMSEncryptionContext = aws.String(s.Config.SSEKMSEncryptionContext)
}
}
func (s *S3) enrichCopyObjectParams(params *s3.CopyObjectInput) {
if s.Config.CheckSumAlgorithm != "" {
params.ChecksumAlgorithm = s3types.ChecksumAlgorithm(s.Config.CheckSumAlgorithm)
}
// https://github.com/Altinity/clickhouse-backup/issues/588
if len(s.Config.ObjectLabels) > 0 {
tags := ""
for k, v := range s.Config.ObjectLabels {
if tags != "" {
tags += "&"
}
tags += k + "=" + v
}
params.Tagging = aws.String(tags)
}
if s.Config.SSE != "" {
params.ServerSideEncryption = s3types.ServerSideEncryption(s.Config.SSE)
}
if s.Config.SSEKMSKeyId != "" {
params.SSEKMSKeyId = aws.String(s.Config.SSEKMSKeyId)
}
if s.Config.SSECustomerAlgorithm != "" {
params.SSECustomerAlgorithm = aws.String(s.Config.SSECustomerAlgorithm)
}
if s.Config.SSECustomerKey != "" {
params.SSECustomerKey = aws.String(s.Config.SSECustomerKey)
}
if s.Config.SSECustomerKeyMD5 != "" {
params.SSECustomerKeyMD5 = aws.String(s.Config.SSECustomerKeyMD5)
}
if s.Config.SSEKMSEncryptionContext != "" {
params.SSEKMSEncryptionContext = aws.String(s.Config.SSEKMSEncryptionContext)
}
if s.Config.RequestPayer != "" {
params.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
}
func (s *S3) restoreObject(ctx context.Context, key string) error {
restoreRequest := &s3.RestoreObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(path.Join(s.Config.Path, key)),
RestoreRequest: &s3types.RestoreRequest{
Days: aws.Int32(1),
GlacierJobParameters: &s3types.GlacierJobParameters{
Tier: s3types.Tier("Expedited"),
},
},
}
if s.Config.RequestPayer != "" {
restoreRequest.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
_, err := s.client.RestoreObject(ctx, restoreRequest)
if err != nil {
return err
}
i := 0
for {
restoreHeadParams := &s3.HeadObjectInput{
Bucket: aws.String(s.Config.Bucket),
Key: aws.String(path.Join(s.Config.Path, key)),
}
s.enrichHeadParams(restoreHeadParams)
res, err := s.client.HeadObject(ctx, restoreHeadParams)
if err != nil {
return fmt.Errorf("restoreObject: failed to head %s object metadata, %v", path.Join(s.Config.Path, key), err)
}
if res.Restore != nil && *res.Restore == "ongoing-request=\"true\"" {
i += 1
s.Log.Warnf("%s still not restored, will wait %d seconds", key, i*5)
time.Sleep(time.Duration(i*5) * time.Second)
} else {
return nil
}
}
}
func (s *S3) enrichHeadParams(headParams *s3.HeadObjectInput) {
if s.Config.RequestPayer != "" {
headParams.RequestPayer = s3types.RequestPayer(s.Config.RequestPayer)
}
if s.Config.SSECustomerAlgorithm != "" {
headParams.SSECustomerAlgorithm = aws.String(s.Config.SSECustomerAlgorithm)
}
if s.Config.SSECustomerKey != "" {
headParams.SSECustomerKey = aws.String(s.Config.SSECustomerKey)
}
if s.Config.SSECustomerKeyMD5 != "" {
headParams.SSECustomerKeyMD5 = aws.String(s.Config.SSECustomerKeyMD5)
}
}
type s3File struct {
size int64
lastModified time.Time
storageClass string
name string
}
func (f *s3File) Size() int64 {
return f.size
}
func (f *s3File) Name() string {
return f.name
}
func (f *s3File) LastModified() time.Time {
return f.lastModified
}
func (f *s3File) StorageClass() string {
return f.storageClass
}