-
Notifications
You must be signed in to change notification settings - Fork 342
/
adapter.go
871 lines (786 loc) · 26.4 KB
/
adapter.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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
package s3
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/stats"
)
const (
DefaultStreamingChunkSize = 2 << 19 // 1MiB by default per chunk
DefaultStreamingChunkTimeout = time.Second * 1 // if we haven't read DefaultStreamingChunkSize by this duration, write whatever we have as a chunk
// Chunks smaller than that are only allowed for the last chunk upload
minChunkSize = 8 * 1024
)
var (
ErrS3 = errors.New("s3 error")
ErrMissingETag = fmt.Errorf("%w: missing ETag", ErrS3)
)
type Adapter struct {
clients *ClientCache
httpClient *http.Client
streamingChunkSize int
streamingChunkTimeout time.Duration
respServer string
respServerLock sync.Mutex
ServerSideEncryption string
ServerSideEncryptionKmsKeyID string
preSignedExpiry time.Duration
preSignedRefreshWindow time.Duration
disablePreSigned bool
disablePreSignedUI bool
}
func WithStreamingChunkSize(sz int) func(a *Adapter) {
return func(a *Adapter) {
a.streamingChunkSize = sz
}
}
func WithStreamingChunkTimeout(d time.Duration) func(a *Adapter) {
return func(a *Adapter) {
a.streamingChunkTimeout = d
}
}
func WithStatsCollector(s stats.Collector) func(a *Adapter) {
return func(a *Adapter) {
a.clients.SetStatsCollector(s)
}
}
func WithDiscoverBucketRegion(b bool) func(a *Adapter) {
return func(a *Adapter) {
a.clients.DiscoverBucketRegion(b)
}
}
func WithPreSignedRefreshWindow(v time.Duration) func(a *Adapter) {
return func(a *Adapter) {
a.preSignedRefreshWindow = v
}
}
func WithPreSignedExpiry(v time.Duration) func(a *Adapter) {
return func(a *Adapter) {
a.preSignedExpiry = v
}
}
func WithDisablePreSigned(b bool) func(a *Adapter) {
return func(a *Adapter) {
if b {
a.disablePreSigned = true
}
}
}
func WithDisablePreSignedUI(b bool) func(a *Adapter) {
return func(a *Adapter) {
if b {
a.disablePreSignedUI = true
}
}
}
func WithServerSideEncryption(s string) func(a *Adapter) {
return func(a *Adapter) {
a.ServerSideEncryption = s
}
}
func WithServerSideEncryptionKmsKeyID(s string) func(a *Adapter) {
return func(a *Adapter) {
a.ServerSideEncryptionKmsKeyID = s
}
}
type AdapterOption func(a *Adapter)
func NewAdapter(awsSession *session.Session, opts ...AdapterOption) *Adapter {
a := &Adapter{
clients: NewClientCache(awsSession),
httpClient: awsSession.Config.HTTPClient,
streamingChunkSize: DefaultStreamingChunkSize,
streamingChunkTimeout: DefaultStreamingChunkTimeout,
preSignedExpiry: block.DefaultPreSignExpiryDuration,
}
for _, opt := range opts {
opt(a)
}
return a
}
func (a *Adapter) log(ctx context.Context) logging.Logger {
return logging.FromContext(ctx)
}
func (a *Adapter) Put(ctx context.Context, obj block.ObjectPointer, sizeBytes int64, reader io.Reader, opts block.PutOpts) error {
var err error
defer reportMetrics("Put", time.Now(), &sizeBytes, &err)
// for unknown size, we assume we like to stream content, will use s3manager to perform the request.
// we assume the caller may not have 1:1 request to s3 put object in this case as it may perform multipart upload
if sizeBytes == -1 {
return a.managerUpload(ctx, obj, reader, opts)
}
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return err
}
putObject := s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
StorageClass: opts.StorageClass,
}
if a.ServerSideEncryption != "" {
putObject.SetServerSideEncryption(a.ServerSideEncryption)
}
if a.ServerSideEncryptionKmsKeyID != "" {
putObject.SetSSEKMSKeyId(a.ServerSideEncryptionKmsKeyID)
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
sdkRequest, _ := client.PutObjectRequest(&putObject)
headers, err := a.streamToS3(ctx, sdkRequest, sizeBytes, reader)
if err != nil {
return err
}
etag := headers.Get("ETag")
if etag == "" {
return ErrMissingETag
}
return err
}
func (a *Adapter) UploadPart(ctx context.Context, obj block.ObjectPointer, sizeBytes int64, reader io.Reader, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
var err error
defer reportMetrics("UploadPart", time.Now(), &sizeBytes, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return nil, err
}
uploadPartObject := s3.UploadPartInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
PartNumber: aws.Int64(int64(partNumber)),
UploadId: aws.String(uploadID),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
sdkRequest, _ := client.UploadPartRequest(&uploadPartObject)
headers, err := a.streamToS3(ctx, sdkRequest, sizeBytes, reader)
if err != nil {
return nil, err
}
etag := headers.Get("ETag")
if etag == "" {
return nil, ErrMissingETag
}
return &block.UploadPartResponse{
ETag: strings.Trim(etag, `"`),
ServerSideHeader: extractAmzServerSideHeader(headers),
}, nil
}
func (a *Adapter) streamToS3(ctx context.Context, sdkRequest *request.Request, sizeBytes int64, reader io.Reader) (http.Header, error) {
sigTime := time.Now()
log := a.log(ctx).WithField("operation", "PutObject")
if err := sdkRequest.Build(); err != nil {
return nil, err
}
req, err := http.NewRequest(sdkRequest.HTTPRequest.Method, sdkRequest.HTTPRequest.URL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Encoding", StreamingContentEncoding)
req.Header.Set("Transfer-Encoding", "chunked")
req.Header.Set("x-amz-content-sha256", StreamingSha256)
req.Header.Set("x-amz-decoded-content-length", fmt.Sprintf("%d", sizeBytes))
if a.ServerSideEncryption != "" {
req.Header.Set("x-amz-server-side-encryption", a.ServerSideEncryption)
}
if a.ServerSideEncryptionKmsKeyID != "" {
req.Header.Set("x-amz-server-side-encryption-aws-kms-key-id", a.ServerSideEncryptionKmsKeyID)
}
req = req.WithContext(ctx)
baseSigner := v4.NewSigner(sdkRequest.Config.Credentials)
baseSigner.DisableURIPathEscaping = true
_, err = baseSigner.Sign(req, nil, s3.ServiceName, aws.StringValue(sdkRequest.Config.Region), sigTime)
if err != nil {
log.WithError(err).Error("failed to sign request")
return nil, err
}
req.Header.Set("Expect", "100-Continue")
sigSeed, err := v4.GetSignedRequestSignature(req)
if err != nil {
log.WithError(err).Error("failed to get seed signature")
return nil, err
}
req.Body = io.NopCloser(&StreamingReader{
Reader: reader,
Size: sizeBytes,
Time: sigTime,
StreamSigner: v4.NewStreamSigner(
aws.StringValue(sdkRequest.Config.Region),
s3.ServiceName,
sigSeed,
sdkRequest.Config.Credentials,
),
ChunkSize: a.streamingChunkSize,
ChunkTimeout: a.streamingChunkTimeout,
})
resp, err := a.httpClient.Do(req)
if err != nil {
log.WithError(err).
WithField("url", sdkRequest.HTTPRequest.URL.String()).
Error("error making request")
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("%w: %d %s (unknown)", ErrS3, resp.StatusCode, resp.Status)
} else {
err = fmt.Errorf("%w: %s", ErrS3, body)
}
log.WithError(err).
WithField("url", sdkRequest.HTTPRequest.URL.String()).
WithField("status_code", resp.StatusCode).
Error("bad S3 PutObject response")
return nil, err
}
a.extractS3Server(resp)
return resp.Header, nil
}
func isErrNotFound(err error) bool {
var reqErr awserr.RequestFailure
return errors.As(err, &reqErr) && reqErr.StatusCode() == http.StatusNotFound
}
func (a *Adapter) Get(ctx context.Context, obj block.ObjectPointer, _ int64) (io.ReadCloser, error) {
var err error
var sizeBytes int64
defer reportMetrics("Get", time.Now(), &sizeBytes, &err)
log := a.log(ctx).WithField("operation", "GetObject")
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return nil, err
}
getObjectInput := s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
objectOutput, err := client.GetObjectWithContext(ctx, &getObjectInput)
if isErrNotFound(err) {
return nil, block.ErrDataNotFound
}
if err != nil {
log.WithError(err).Errorf("failed to get S3 object bucket %s key %s", qualifiedKey.GetStorageNamespace(), qualifiedKey.GetKey())
return nil, err
}
sizeBytes = aws.Int64Value(objectOutput.ContentLength)
return objectOutput.Body, nil
}
func (a *Adapter) GetWalker(uri *url.URL) (block.Walker, error) {
if err := block.ValidateStorageType(uri, block.StorageTypeS3); err != nil {
return nil, err
}
return NewS3Walker(a.clients.awsSession), nil
}
// refreshClientIfNeeded ensure client has some time before its token
// expires. It returns the updated expiry time. It fails with
// ErrDoesntExpire if the client is not an Expirer,
func refreshClientIfNeeded(ctx context.Context, client S3APIWithExpirer, refreshWindow time.Duration) (time.Time, error) {
if client == nil {
return time.Time{}, nil
}
expiry, err := client.ExpiresAt()
if errors.Is(err, ErrDoesntExpire) {
return time.Time{}, ErrDoesntExpire
} else if err != nil {
return time.Time{}, fmt.Errorf("refresh client if needed: get current expiry: %w", err)
}
ttl := time.Until(expiry)
l := logging.FromContext(ctx).WithFields(logging.Fields{
"expiry": expiry,
"TTL": ttl.String(),
})
if ttl < refreshWindow {
l.Info("Refresh client as it will expire soon")
expiry, err = client.Refresh()
if err != nil {
return time.Time{}, fmt.Errorf("refresh client if needed: refreshing: %w", err)
}
ttl = time.Until(expiry)
l = l.WithFields(logging.Fields{
"expiry": expiry,
"TTL": ttl.String(),
})
l.Info("Refreshed client")
}
l.Trace("Got client")
return expiry, nil
}
func (a *Adapter) GetPreSignedURL(ctx context.Context, obj block.ObjectPointer, mode block.PreSignMode) (string, time.Time, error) {
if a.disablePreSigned {
return "", time.Time{}, block.ErrOperationNotSupported
}
log := a.log(ctx).WithField("operation", "GetPreSignedURL")
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
log.WithField("namespace", obj.StorageNamespace).
WithField("identifier", obj.Identifier).
WithError(err).Error("could not resolve namespace")
return "", time.Time{}, err
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
clientExpiry, clientExpiryErr := refreshClientIfNeeded(ctx, client, a.preSignedRefreshWindow)
expiry := time.Now().Add(a.preSignedExpiry)
log = log.WithField("expiry", expiry)
switch {
case clientExpiryErr == nil:
if clientExpiry.Before(expiry) && !clientExpiry.IsZero() {
log.WithField("client_expiry", clientExpiry).
Trace("URL expiry shortened by client expiry")
// TODO(ariels): Monitor this?
expiry = clientExpiry
log = log.WithField("expiry", expiry)
}
case errors.Is(clientExpiryErr, ErrDoesntExpire):
break
default:
log.WithFields(logging.Fields{
"namespace": obj.StorageNamespace,
"identifier": obj.Identifier,
}).
WithError(err).
Warning("Failed to get client (token) expiry: URL expiry may be too high")
}
// BUG(ariels): This is an inherent race. urlLifetime is computed
// relative to the local clock. If expiry was shortened because
// of clientExpiry then AWS will determine _remotely_ whether
// the URL expired. So this URL can expire before the client or
// even lakeFS think that it has.
//
// This is a limitation of the AWS SDK, which signs locally, and
// of the AWS S3 API, which does not allow a meaningful
// workaround.
urlLifetime := time.Until(expiry)
log = log.WithField("TTL", urlLifetime)
var preSignedURL string
if mode == block.PreSignModeWrite {
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
req, _ := client.PutObjectRequest(putObjectInput)
preSignedURL, err = req.Presign(urlLifetime)
} else {
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
req, _ := client.GetObjectRequest(getObjectInput)
preSignedURL, err = req.Presign(urlLifetime)
}
if err != nil {
log.WithField("namespace", obj.StorageNamespace).
WithField("identifier", obj.Identifier).
WithError(err).Error("could not pre-sign request")
}
return preSignedURL, expiry, err
}
func (a *Adapter) Exists(ctx context.Context, obj block.ObjectPointer) (bool, error) {
var err error
defer reportMetrics("Exists", time.Now(), nil, &err)
log := a.log(ctx).WithField("operation", "HeadObject")
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return false, err
}
input := s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
_, err = client.HeadObjectWithContext(ctx, &input)
if isErrNotFound(err) {
return false, nil
}
if err != nil {
log.WithError(err).Errorf("failed to stat S3 object")
return false, err
}
return true, nil
}
func (a *Adapter) GetRange(ctx context.Context, obj block.ObjectPointer, startPosition int64, endPosition int64) (io.ReadCloser, error) {
var err error
var sizeBytes int64
defer reportMetrics("GetRange", time.Now(), &sizeBytes, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return nil, err
}
log := a.log(ctx).WithField("operation", "GetObjectRange")
getObjectInput := s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Range: aws.String(fmt.Sprintf("bytes=%d-%d", startPosition, endPosition)),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
objectOutput, err := client.GetObjectWithContext(ctx, &getObjectInput)
if isErrNotFound(err) {
return nil, block.ErrDataNotFound
}
if err != nil {
log.WithError(err).WithFields(logging.Fields{
"start_position": startPosition,
"end_position": endPosition,
}).Error("failed to get S3 object range")
return nil, err
}
sizeBytes = aws.Int64Value(objectOutput.ContentLength)
return objectOutput.Body, nil
}
func (a *Adapter) GetProperties(ctx context.Context, obj block.ObjectPointer) (block.Properties, error) {
var err error
defer reportMetrics("GetProperties", time.Now(), nil, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return block.Properties{}, err
}
headObjectParams := &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
s3Props, err := client.HeadObjectWithContext(ctx, headObjectParams)
if err != nil {
return block.Properties{}, err
}
return block.Properties{StorageClass: s3Props.StorageClass}, nil
}
func (a *Adapter) Remove(ctx context.Context, obj block.ObjectPointer) error {
var err error
defer reportMetrics("Remove", time.Now(), nil, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return err
}
deleteObjectParams := &s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
svc := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
_, err = svc.DeleteObjectWithContext(ctx, deleteObjectParams)
if err != nil {
a.log(ctx).WithError(err).Error("failed to delete S3 object")
return err
}
err = svc.WaitUntilObjectNotExistsWithContext(ctx, &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
return err
}
func (a *Adapter) copyPart(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int, byteRange *string) (*block.UploadPartResponse, error) {
srcKey, err := resolveNamespace(sourceObj)
if err != nil {
return nil, err
}
bucket, key, qualifiedKey, err := a.extractParamsFromObj(destinationObj)
if err != nil {
return nil, err
}
uploadPartCopyObject := s3.UploadPartCopyInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
PartNumber: aws.Int64(int64(partNumber)),
UploadId: aws.String(uploadID),
CopySource: aws.String(fmt.Sprintf("%s/%s", srcKey.GetStorageNamespace(), srcKey.GetKey())),
}
if byteRange != nil {
uploadPartCopyObject.CopySourceRange = byteRange
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
req, resp := client.UploadPartCopyRequest(&uploadPartCopyObject)
req.SetContext(ctx)
err = req.Send()
if err != nil {
return nil, err
}
if resp == nil || resp.CopyPartResult == nil || resp.CopyPartResult.ETag == nil {
return nil, ErrMissingETag
}
etag := strings.Trim(*resp.CopyPartResult.ETag, `"`)
// x-amz-server-side-* headers
headers := make(http.Header)
for k, v := range req.HTTPResponse.Header {
if strings.HasPrefix(k, "X-Amz-Server-Side-") {
headers[k] = v
}
}
return &block.UploadPartResponse{
ETag: etag,
ServerSideHeader: headers,
}, nil
}
func (a *Adapter) UploadCopyPart(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
var err error
defer reportMetrics("UploadCopyPart", time.Now(), nil, &err)
return a.copyPart(ctx, sourceObj, destinationObj, uploadID, partNumber, nil)
}
func (a *Adapter) UploadCopyPartRange(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int, startPosition, endPosition int64) (*block.UploadPartResponse, error) {
var err error
defer reportMetrics("UploadCopyPartRange", time.Now(), nil, &err)
return a.copyPart(ctx,
sourceObj, destinationObj, uploadID, partNumber,
aws.String(fmt.Sprintf("bytes=%d-%d", startPosition, endPosition)))
}
func (a *Adapter) Copy(ctx context.Context, sourceObj, destinationObj block.ObjectPointer) error {
var err error
defer reportMetrics("Copy", time.Now(), nil, &err)
qualifiedSourceKey, err := resolveNamespace(sourceObj)
if err != nil {
return err
}
destBucket, destKey, _, err := a.extractParamsFromObj(destinationObj)
if err != nil {
return err
}
copyObjectParams := &s3.CopyObjectInput{
Bucket: aws.String(destBucket),
Key: aws.String(destKey),
CopySource: aws.String(qualifiedSourceKey.GetStorageNamespace() + "/" + qualifiedSourceKey.GetKey()),
}
if a.ServerSideEncryption != "" {
copyObjectParams.SetServerSideEncryption(a.ServerSideEncryption)
}
if a.ServerSideEncryptionKmsKeyID != "" {
copyObjectParams.SetSSEKMSKeyId(a.ServerSideEncryptionKmsKeyID)
}
_, err = a.clients.Get(ctx, destBucket).CopyObjectWithContext(ctx, copyObjectParams)
if err != nil {
a.log(ctx).WithError(err).Error("failed to copy S3 object")
}
return err
}
func (a *Adapter) CreateMultiPartUpload(ctx context.Context, obj block.ObjectPointer, _ *http.Request, opts block.CreateMultiPartUploadOpts) (*block.CreateMultiPartUploadResponse, error) {
var err error
defer reportMetrics("CreateMultiPartUpload", time.Now(), nil, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return nil, err
}
input := &s3.CreateMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ContentType: aws.String(""),
StorageClass: opts.StorageClass,
}
if a.ServerSideEncryption != "" {
input.SetServerSideEncryption(a.ServerSideEncryption)
}
if a.ServerSideEncryptionKmsKeyID != "" {
input.SetSSEKMSKeyId(a.ServerSideEncryptionKmsKeyID)
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
req, resp := client.CreateMultipartUploadRequest(input)
req.SetContext(ctx)
err = req.Send()
if err != nil {
return nil, err
}
uploadID := aws.StringValue(resp.UploadId)
a.log(ctx).WithFields(logging.Fields{
"upload_id": uploadID,
"qualified_ns": qualifiedKey.GetStorageNamespace(),
"qualified_key": qualifiedKey.GetKey(),
"key": obj.Identifier,
}).Debug("created multipart upload")
return &block.CreateMultiPartUploadResponse{
UploadID: uploadID,
ServerSideHeader: extractAmzServerSideHeader(req.HTTPResponse.Header),
}, err
}
func (a *Adapter) AbortMultiPartUpload(ctx context.Context, obj block.ObjectPointer, uploadID string) error {
var err error
defer reportMetrics("AbortMultiPartUpload", time.Now(), nil, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return err
}
input := &s3.AbortMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
UploadId: aws.String(uploadID),
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
_, err = client.AbortMultipartUploadWithContext(ctx, input)
lg := a.log(ctx).WithFields(logging.Fields{
"upload_id": uploadID,
"qualified_ns": qualifiedKey.GetStorageNamespace(),
"qualified_key": qualifiedKey.GetKey(),
"key": obj.Identifier,
})
if err != nil {
lg.Error("Failed to abort multipart upload")
return err
}
lg.Debug("aborted multipart upload")
return nil
}
func convertFromBlockMultipartUploadCompletion(multipartList *block.MultipartUploadCompletion) *s3.CompletedMultipartUpload {
parts := make([]*s3.CompletedPart, len(multipartList.Part))
for i, p := range multipartList.Part {
parts[i] = &s3.CompletedPart{
ETag: aws.String(p.ETag),
PartNumber: aws.Int64(int64(p.PartNumber)),
}
}
return &s3.CompletedMultipartUpload{Parts: parts}
}
func (a *Adapter) CompleteMultiPartUpload(ctx context.Context, obj block.ObjectPointer, uploadID string, multipartList *block.MultipartUploadCompletion) (*block.CompleteMultiPartUploadResponse, error) {
var err error
defer reportMetrics("CompleteMultiPartUpload", time.Now(), nil, &err)
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return nil, err
}
input := &s3.CompleteMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
UploadId: aws.String(uploadID),
MultipartUpload: convertFromBlockMultipartUploadCompletion(multipartList),
}
lg := a.log(ctx).WithFields(logging.Fields{
"upload_id": uploadID,
"qualified_ns": qualifiedKey.GetStorageNamespace(),
"qualified_key": qualifiedKey.GetKey(),
"key": obj.Identifier,
})
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
req, resp := client.CompleteMultipartUploadRequest(input)
req.SetContext(ctx)
err = req.Send()
if err != nil {
lg.WithError(err).Error("CompleteMultipartUpload failed")
return nil, err
}
lg.Debug("completed multipart upload")
headInput := &s3.HeadObjectInput{Bucket: &bucket, Key: &key}
headResp, err := client.HeadObjectWithContext(ctx, headInput)
if err != nil {
return nil, err
}
etag := strings.Trim(aws.StringValue(resp.ETag), `"`)
contentLength := aws.Int64Value(headResp.ContentLength)
return &block.CompleteMultiPartUploadResponse{
ETag: etag,
ContentLength: contentLength,
ServerSideHeader: extractAmzServerSideHeader(req.HTTPResponse.Header),
}, nil
}
func (a *Adapter) BlockstoreType() string {
return block.BlockstoreTypeS3
}
func (a *Adapter) GetStorageNamespaceInfo() block.StorageNamespaceInfo {
info := block.DefaultStorageNamespaceInfo(block.BlockstoreTypeS3)
if a.disablePreSigned {
info.PreSignSupport = false
}
if !(a.disablePreSignedUI || a.disablePreSigned) {
info.PreSignSupportUI = true
}
return info
}
func resolveNamespace(obj block.ObjectPointer) (block.CommonQualifiedKey, error) {
qualifiedKey, err := block.DefaultResolveNamespace(obj.StorageNamespace, obj.Identifier, obj.IdentifierType)
if err != nil {
return qualifiedKey, err
}
if qualifiedKey.GetStorageType() != block.StorageTypeS3 {
return qualifiedKey, fmt.Errorf("expected storage type s3: %w", block.ErrInvalidAddress)
}
return qualifiedKey, nil
}
func (a *Adapter) ResolveNamespace(storageNamespace, key string, identifierType block.IdentifierType) (block.QualifiedKey, error) {
return block.DefaultResolveNamespace(storageNamespace, key, identifierType)
}
func (a *Adapter) RuntimeStats() map[string]string {
a.respServerLock.Lock()
defer a.respServerLock.Unlock()
if a.respServer == "" {
return nil
}
return map[string]string{
"resp_server": a.respServer,
}
}
func (a *Adapter) extractS3Server(resp *http.Response) {
if resp == nil || resp.Header == nil {
return
}
// Extract the responding server from the response.
// Expected values: "S3" from AWS, "MinIO" for MinIO. Others unknown.
server := resp.Header.Get("Server")
if server == "" {
return
}
a.respServerLock.Lock()
defer a.respServerLock.Unlock()
a.respServer = server
}
func (a *Adapter) managerUpload(ctx context.Context, obj block.ObjectPointer, reader io.Reader, opts block.PutOpts) error {
bucket, key, qualifiedKey, err := a.extractParamsFromObj(obj)
if err != nil {
return err
}
client := a.clients.Get(ctx, qualifiedKey.GetStorageNamespace())
uploader := s3manager.NewUploaderWithClient(client)
input := &s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: reader,
StorageClass: opts.StorageClass,
}
if a.ServerSideEncryption != "" {
input.ServerSideEncryption = aws.String(a.ServerSideEncryption)
}
if a.ServerSideEncryptionKmsKeyID != "" {
input.SSEKMSKeyId = aws.String(a.ServerSideEncryptionKmsKeyID)
}
output, err := uploader.UploadWithContext(ctx, input)
if err != nil {
return err
}
if aws.StringValue(output.ETag) == "" {
return ErrMissingETag
}
return nil
}
func extractAmzServerSideHeader(header http.Header) http.Header {
// return additional headers: x-amz-server-side-*
h := make(http.Header)
for k, v := range header {
if strings.HasPrefix(k, "X-Amz-Server-Side-") {
h[k] = v
}
}
return h
}
func (a *Adapter) extractParamsFromObj(obj block.ObjectPointer) (string, string, block.QualifiedKey, error) {
qk, err := a.ResolveNamespace(obj.StorageNamespace, obj.Identifier, obj.IdentifierType)
if err != nil {
return "", "", nil, err
}
bucket, key := ExtractParamsFromQK(qk)
return bucket, key, qk, nil
}
func ExtractParamsFromQK(qk block.QualifiedKey) (string, string) {
bucket, prefix, _ := strings.Cut(qk.GetStorageNamespace(), "/")
key := qk.GetKey()
if len(prefix) > 0 { // Avoid situations where prefix is empty or "/"
key = prefix + "/" + key
}
return bucket, key
}