-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathapi_op_UploadObject.go
More file actions
1175 lines (1027 loc) · 43.9 KB
/
api_op_UploadObject.go
File metadata and controls
1175 lines (1027 loc) · 43.9 KB
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package transfermanager
import (
"bytes"
"context"
"fmt"
"io"
"log"
"sort"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-sdk-go-v2/feature/s3/transfermanager/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
smithymiddleware "github.com/aws/smithy-go/middleware"
)
// A MultipartUploadError wraps a failed S3 multipart upload. An error returned
// will satisfy this interface when a multi part upload failed to upload all
// chucks to S3. In the case of a failure the UploadID is needed to operate on
// the chunks, if any, which were uploaded.
//
// Example:
//
// c := transfermanager.New(client, opts)
// output, err := c.PutObject(context.Background(), input)
// if err != nil {
// var multierr transfermanager.MultipartUploadError
// if errors.As(err, &multierr) {
// fmt.Printf("upload failure UploadID=%s, %s\n", multierr.UploadID(), multierr.Error())
// } else {
// fmt.Printf("upload failure, %s\n", err.Error())
// }
// }
type MultipartUploadError interface {
error
// UploadID returns the upload id for the S3 multipart upload that failed.
UploadID() string
}
// A multipartUploadError wraps the upload ID of a failed s3 multipart upload.
// Composed of BaseError for code, message, and original error
//
// Should be used for an error that occurred failing a S3 multipart upload,
// and a upload ID is available.
type multipartUploadError struct {
err error
// ID for multipart upload which failed.
uploadID string
}
// Error returns the string representation of the error.
//
// Satisfies the error interface.
func (m *multipartUploadError) Error() string {
var extra string
if m.err != nil {
extra = fmt.Sprintf(", cause: %s", m.err.Error())
}
return fmt.Sprintf("upload multipart failed, upload id: %s%s", m.uploadID, extra)
}
// Unwrap returns the underlying error that cause the upload failure
func (m *multipartUploadError) Unwrap() error {
return m.err
}
// UploadID returns the id of the S3 upload which failed.
func (m *multipartUploadError) UploadID() string {
return m.uploadID
}
// UploadObjectInput represents a request to the PutObject() call. It contains common fields
// of s3 PutObject and CreateMultipartUpload input
type UploadObjectInput struct {
// Bucket the object is uploaded into
Bucket *string
// Object key for which the PUT action was initiated
Key *string
// Object data
Body io.Reader
// The canned ACL to apply to the object. For more information, see [Canned ACL] in the Amazon
// S3 User Guide.
//
// When adding a new object, you can use headers to grant ACL-based permissions to
// individual Amazon Web Services accounts or to predefined groups defined by
// Amazon S3. These permissions are then added to the ACL on the object. By
// default, all objects are private. Only the owner has full access control. For
// more information, see [Access Control List (ACL) Overview]and [Managing ACLs Using the REST API] in the Amazon S3 User Guide.
//
// If the bucket that you're uploading objects to uses the bucket owner enforced
// setting for S3 Object Ownership, ACLs are disabled and no longer affect
// permissions. Buckets that use this setting only accept PUT requests that don't
// specify an ACL or PUT requests that specify bucket owner full control ACLs, such
// as the bucket-owner-full-control canned ACL or an equivalent form of this ACL
// expressed in the XML format. PUT requests that contain other ACLs (for example,
// custom grants to certain Amazon Web Services accounts) fail and return a 400
// error with the error code AccessControlListNotSupported . For more information,
// see [Controlling ownership of objects and disabling ACLs]in the Amazon S3 User Guide.
//
// - This functionality is not supported for directory buckets.
//
// - This functionality is not supported for Amazon S3 on Outposts.
//
// [Managing ACLs Using the REST API]: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-using-rest-api.html
// [Access Control List (ACL) Overview]: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
// [Canned ACL]: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#CannedACL
// [Controlling ownership of objects and disabling ACLs]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/about-object-ownership.html
ACL types.ObjectCannedACL
// Specifies whether Amazon S3 should use an S3 Bucket Key for object encryption
// with server-side encryption using Key Management Service (KMS) keys (SSE-KMS).
// Setting this header to true causes Amazon S3 to use an S3 Bucket Key for object
// encryption with SSE-KMS.
//
// Specifying this header with a PUT action doesn’t affect bucket-level settings
// for S3 Bucket Key.
//
// This functionality is not supported for directory buckets.
BucketKeyEnabled *bool
// Can be used to specify caching behavior along the request/reply chain. For more
// information, see [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9].
//
// [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
CacheControl *string
// Indicates the algorithm used to create the checksum for the object when you use
// the SDK. This header will not provide any additional functionality if you don't
// use the SDK. When you send this header, there must be a corresponding
// x-amz-checksum-algorithm or x-amz-trailer header sent. Otherwise, Amazon S3
// fails the request with the HTTP status code 400 Bad Request .
//
// For the x-amz-checksum-algorithm header, replace algorithm with the
// supported algorithm from the following list:
//
// - CRC32
//
// - CRC32C
//
// - SHA1
//
// - SHA256
//
// For more information, see [Checking object integrity] in the Amazon S3 User Guide.
//
// If the individual checksum value you provide through x-amz-checksum-algorithm
// doesn't match the checksum algorithm you set through
// x-amz-sdk-checksum-algorithm , Amazon S3 ignores any provided ChecksumAlgorithm
// parameter and uses the checksum algorithm that matches the provided value in
// x-amz-checksum-algorithm .
//
// For directory buckets, when you use Amazon Web Services SDKs, CRC32 is the
// default checksum algorithm that's used for performance.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumAlgorithm types.ChecksumAlgorithm
// This header can be used as a data integrity check to verify that the data
// received is the same data that was originally sent. This header specifies the
// Base64 encoded, 32-bit CRC32 checksum of the object. For more information, see [Checking object integrity]
// in the Amazon S3 User Guide.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumCRC32 *string
// This header can be used as a data integrity check to verify that the data
// received is the same data that was originally sent. This header specifies the
// Base64 encoded, 32-bit CRC32C checksum of the object. For more information, see [Checking object integrity]
// in the Amazon S3 User Guide.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumCRC32C *string
// This header can be used as a data integrity check to verify that the data
// received is the same data that was originally sent. This header specifies the
// Base64 encoded, 64-bit CRC64NVME checksum of the object. The CRC64NVME checksum
// is always a full object checksum. For more information, see [Checking object integrity in the Amazon S3 User Guide].
//
// [Checking object integrity in the Amazon S3 User Guide]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumCRC64NVME *string
// This header can be used as a data integrity check to verify that the data
// received is the same data that was originally sent. This header specifies the
// Base64 encoded, 160-bit SHA1 digest of the object. For more information, see [Checking object integrity]
// in the Amazon S3 User Guide.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumSHA1 *string
// This header can be used as a data integrity check to verify that the data
// received is the same data that was originally sent. This header specifies the
// Base64 encoded, 256-bit SHA256 digest of the object. For more information, see [Checking object integrity]
// in the Amazon S3 User Guide.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumSHA256 *string
// Size of the body in bytes. This parameter is useful when the size of the body
// cannot be determined automatically. For more information, see [https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length].
//
// [https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length]: https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length
ContentLength *int64
// Specifies presentational information for the object. For more information, see [https://www.rfc-editor.org/rfc/rfc6266#section-4].
//
// [https://www.rfc-editor.org/rfc/rfc6266#section-4]: https://www.rfc-editor.org/rfc/rfc6266#section-4
ContentDisposition *string
// Specifies what content encodings have been applied to the object and thus what
// decoding mechanisms must be applied to obtain the media-type referenced by the
// Content-Type header field. For more information, see [https://www.rfc-editor.org/rfc/rfc9110.html#field.content-encoding].
//
// [https://www.rfc-editor.org/rfc/rfc9110.html#field.content-encoding]: https://www.rfc-editor.org/rfc/rfc9110.html#field.content-encoding
ContentEncoding *string
// The language the content is in.
ContentLanguage *string
// A standard MIME type describing the format of the contents. For more
// information, see [https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type].
//
// [https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type]: https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type
ContentType *string
// The account ID of the expected bucket owner. If the account ID that you provide
// does not match the actual owner of the bucket, the request fails with the HTTP
// status code 403 Forbidden (access denied).
ExpectedBucketOwner *string
// The date and time at which the object is no longer cacheable. For more
// information, see [https://www.rfc-editor.org/rfc/rfc7234#section-5.3].
//
// [https://www.rfc-editor.org/rfc/rfc7234#section-5.3]: https://www.rfc-editor.org/rfc/rfc7234#section-5.3
Expires *time.Time
// Gives the grantee READ, READ_ACP, and WRITE_ACP permissions on the object.
//
// - This functionality is not supported for directory buckets.
//
// - This functionality is not supported for Amazon S3 on Outposts.
GrantFullControl *string
// Allows grantee to read the object data and its metadata.
//
// - This functionality is not supported for directory buckets.
//
// - This functionality is not supported for Amazon S3 on Outposts.
GrantRead *string
// Allows grantee to read the object ACL.
//
// - This functionality is not supported for directory buckets.
//
// - This functionality is not supported for Amazon S3 on Outposts.
GrantReadACP *string
// Allows grantee to write the ACL for the applicable object.
//
// - This functionality is not supported for directory buckets.
//
// - This functionality is not supported for Amazon S3 on Outposts.
GrantWriteACP *string
// Uploads the object only if the ETag (entity tag) value provided during the
// WRITE operation matches the ETag of the object in S3. If the ETag values do not
// match, the operation returns a 412 Precondition Failed error.
//
// If a conflicting operation occurs during the upload S3 returns a 409
// ConditionalRequestConflict response. On a 409 failure you should fetch the
// object's ETag and retry the upload.
//
// Expects the ETag value as a string.
//
// For more information about conditional requests, see [RFC 7232], or [Conditional requests] in the Amazon S3
// User Guide.
//
// [Conditional requests]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-requests.html
// [RFC 7232]: https://tools.ietf.org/html/rfc7232
IfMatch *string
// Uploads the object only if the object key name does not already exist in the
// bucket specified. Otherwise, Amazon S3 returns a 412 Precondition Failed error.
//
// If a conflicting operation occurs during the upload S3 returns a 409
// ConditionalRequestConflict response. On a 409 failure you should retry the
// upload.
//
// Expects the '*' (asterisk) character.
//
// For more information about conditional requests, see [RFC 7232], or [Conditional requests] in the Amazon S3
// User Guide.
//
// [Conditional requests]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-requests.html
// [RFC 7232]: https://tools.ietf.org/html/rfc7232
IfNoneMatch *string
// A map of metadata to store with the object in S3.
Metadata map[string]string
// Specifies whether a legal hold will be applied to this object. For more
// information about S3 Object Lock, see [Object Lock]in the Amazon S3 User Guide.
//
// This functionality is not supported for directory buckets.
//
// [Object Lock]: https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock.html
ObjectLockLegalHoldStatus types.ObjectLockLegalHoldStatus
// The Object Lock mode that you want to apply to this object.
//
// This functionality is not supported for directory buckets.
ObjectLockMode types.ObjectLockMode
// The date and time when you want this object's Object Lock to expire. Must be
// formatted as a timestamp parameter.
//
// This functionality is not supported for directory buckets.
ObjectLockRetainUntilDate *time.Time
// Confirms that the requester knows that they will be charged for the request.
// Bucket owners need not specify this parameter in their requests. If either the
// source or destination S3 bucket has Requester Pays enabled, the requester will
// pay for corresponding charges to copy the object. For information about
// downloading objects from Requester Pays buckets, see [Downloading Objects in Requester Pays Buckets]in the Amazon S3 User
// Guide.
//
// This functionality is not supported for directory buckets.
//
// [Downloading Objects in Requester Pays Buckets]: https://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectsinRequesterPaysBuckets.html
RequestPayer types.RequestPayer
// Specifies the algorithm to use when encrypting the object (for example, AES256 ).
//
// This functionality is not supported for directory buckets.
SSECustomerAlgorithm *string
// Specifies the customer-provided encryption key for Amazon S3 to use in
// encrypting data. This value is used to store the object and then it is
// discarded; Amazon S3 does not store the encryption key. The key must be
// appropriate for use with the algorithm specified in the
// x-amz-server-side-encryption-customer-algorithm header.
//
// This functionality is not supported for directory buckets.
SSECustomerKey *string
// Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321.
// Amazon S3 uses this header for a message integrity check to ensure that the
// encryption key was transmitted without error.
//
// This functionality is not supported for directory buckets.
SSECustomerKeyMD5 *string
// Specifies the Amazon Web Services KMS Encryption Context to use for object
// encryption. The value of this header is a base64-encoded UTF-8 string holding
// JSON with the encryption context key-value pairs. This value is stored as object
// metadata and automatically gets passed on to Amazon Web Services KMS for future
// GetObject or CopyObject operations on this object. This value must be
// explicitly added during CopyObject operations.
//
// This functionality is not supported for directory buckets.
SSEKMSEncryptionContext *string
// If x-amz-server-side-encryption has a valid value of aws:kms or aws:kms:dsse ,
// this header specifies the ID (Key ID, Key ARN, or Key Alias) of the Key
// Management Service (KMS) symmetric encryption customer managed key that was used
// for the object. If you specify x-amz-server-side-encryption:aws:kms or
// x-amz-server-side-encryption:aws:kms:dsse , but do not provide
// x-amz-server-side-encryption-aws-kms-key-id , Amazon S3 uses the Amazon Web
// Services managed key ( aws/s3 ) to protect the data. If the KMS key does not
// exist in the same account that's issuing the command, you must use the full ARN
// and not just the ID.
//
// This functionality is not supported for directory buckets.
SSEKMSKeyID *string
// The server-side encryption algorithm that was used when you store this object
// in Amazon S3 (for example, AES256 , aws:kms , aws:kms:dsse ).
//
// General purpose buckets - You have four mutually exclusive options to protect
// data using server-side encryption in Amazon S3, depending on how you choose to
// manage the encryption keys. Specifically, the encryption key options are Amazon
// S3 managed keys (SSE-S3), Amazon Web Services KMS keys (SSE-KMS or DSSE-KMS),
// and customer-provided keys (SSE-C). Amazon S3 encrypts data with server-side
// encryption by using Amazon S3 managed keys (SSE-S3) by default. You can
// optionally tell Amazon S3 to encrypt data at rest by using server-side
// encryption with other key options. For more information, see [Using Server-Side Encryption]in the Amazon S3
// User Guide.
//
// Directory buckets - For directory buckets, only the server-side encryption with
// Amazon S3 managed keys (SSE-S3) ( AES256 ) value is supported.
//
// [Using Server-Side Encryption]: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html
ServerSideEncryption types.ServerSideEncryption
// By default, Amazon S3 uses the STANDARD Storage Class to store newly created
// objects. The STANDARD storage class provides high durability and high
// availability. Depending on performance needs, you can specify a different
// Storage Class. For more information, see [Storage Classes]in the Amazon S3 User Guide.
//
// - For directory buckets, only the S3 Express One Zone storage class is
// supported to store newly created objects.
//
// - Amazon S3 on Outposts only uses the OUTPOSTS Storage Class.
//
// [Storage Classes]: https://docs.aws.amazon.com/AmazonS3/latest/dev/storage-class-intro.html
StorageClass types.StorageClass
// The tag-set for the object. The tag-set must be encoded as URL Query
// parameters. (For example, "Key1=Value1")
//
// This functionality is not supported for directory buckets.
Tagging *string
// If the bucket is configured as a website, redirects requests for this object to
// another object in the same bucket or to an external URL. Amazon S3 stores the
// value of this header in the object metadata. For information about object
// metadata, see [Object Key and Metadata]in the Amazon S3 User Guide.
//
// In the following example, the request header sets the redirect to an object
// (anotherPage.html) in the same bucket:
//
// x-amz-website-redirect-location: /anotherPage.html
//
// In the following example, the request header sets the object redirect to
// another website:
//
// x-amz-website-redirect-location: http://www.example.com/
//
// For more information about website hosting in Amazon S3, see [Hosting Websites on Amazon S3] and [How to Configure Website Page Redirects] in the
// Amazon S3 User Guide.
//
// This functionality is not supported for directory buckets.
//
// [How to Configure Website Page Redirects]: https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
// [Hosting Websites on Amazon S3]: https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html
// [Object Key and Metadata]: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
WebsiteRedirectLocation *string
}
// map non-zero string to *string
func nzstring(v string) *string {
if v == "" {
return nil
}
return aws.String(v)
}
// map non-zero Time to *Time
func nztime(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return aws.Time(t)
}
func (i UploadObjectInput) mapSingleUploadInput(body io.Reader, checksumAlgorithm types.ChecksumAlgorithm) *s3.PutObjectInput {
input := &s3.PutObjectInput{
Bucket: i.Bucket,
Key: i.Key,
Body: body,
ACL: s3types.ObjectCannedACL(i.ACL),
BucketKeyEnabled: i.BucketKeyEnabled,
CacheControl: i.CacheControl,
ChecksumCRC32: i.ChecksumCRC32,
ChecksumCRC32C: i.ChecksumCRC32C,
ChecksumCRC64NVME: i.ChecksumCRC64NVME,
ChecksumSHA1: i.ChecksumSHA1,
ChecksumSHA256: i.ChecksumSHA256,
ContentDisposition: i.ContentDisposition,
ContentEncoding: i.ContentEncoding,
ContentLanguage: i.ContentLanguage,
ContentType: i.ContentType,
ExpectedBucketOwner: i.ExpectedBucketOwner,
Expires: i.Expires,
GrantFullControl: i.GrantFullControl,
GrantRead: i.GrantRead,
GrantReadACP: i.GrantReadACP,
GrantWriteACP: i.GrantWriteACP,
IfMatch: i.IfMatch,
IfNoneMatch: i.IfNoneMatch,
Metadata: i.Metadata,
ObjectLockLegalHoldStatus: s3types.ObjectLockLegalHoldStatus(i.ObjectLockLegalHoldStatus),
ObjectLockMode: s3types.ObjectLockMode(i.ObjectLockMode),
ObjectLockRetainUntilDate: i.ObjectLockRetainUntilDate,
RequestPayer: s3types.RequestPayer(i.RequestPayer),
SSECustomerAlgorithm: i.SSECustomerAlgorithm,
SSECustomerKey: i.SSECustomerKey,
SSECustomerKeyMD5: i.SSECustomerKeyMD5,
SSEKMSEncryptionContext: i.SSEKMSEncryptionContext,
SSEKMSKeyId: i.SSEKMSKeyID,
ServerSideEncryption: s3types.ServerSideEncryption(i.ServerSideEncryption),
StorageClass: s3types.StorageClass(i.StorageClass),
Tagging: i.Tagging,
WebsiteRedirectLocation: i.WebsiteRedirectLocation,
}
if i.ChecksumAlgorithm != "" {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(i.ChecksumAlgorithm)
} else {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(checksumAlgorithm)
}
return input
}
func (i UploadObjectInput) mapCreateMultipartUploadInput(checksumAlgorithm types.ChecksumAlgorithm) *s3.CreateMultipartUploadInput {
input := &s3.CreateMultipartUploadInput{
Bucket: i.Bucket,
Key: i.Key,
ACL: s3types.ObjectCannedACL(i.ACL),
BucketKeyEnabled: i.BucketKeyEnabled,
CacheControl: i.CacheControl,
ContentDisposition: i.ContentDisposition,
ContentEncoding: i.ContentEncoding,
ContentLanguage: i.ContentLanguage,
ContentType: i.ContentType,
ExpectedBucketOwner: i.ExpectedBucketOwner,
Expires: i.Expires,
GrantFullControl: i.GrantFullControl,
GrantRead: i.GrantRead,
GrantReadACP: i.GrantReadACP,
GrantWriteACP: i.GrantWriteACP,
Metadata: i.Metadata,
ObjectLockLegalHoldStatus: s3types.ObjectLockLegalHoldStatus(i.ObjectLockLegalHoldStatus),
ObjectLockMode: s3types.ObjectLockMode(i.ObjectLockMode),
ObjectLockRetainUntilDate: i.ObjectLockRetainUntilDate,
RequestPayer: s3types.RequestPayer(i.RequestPayer),
SSECustomerAlgorithm: i.SSECustomerAlgorithm,
SSECustomerKey: i.SSECustomerKey,
SSECustomerKeyMD5: i.SSECustomerKeyMD5,
SSEKMSEncryptionContext: i.SSEKMSEncryptionContext,
SSEKMSKeyId: i.SSEKMSKeyID,
ServerSideEncryption: s3types.ServerSideEncryption(i.ServerSideEncryption),
StorageClass: s3types.StorageClass(i.StorageClass),
Tagging: i.Tagging,
WebsiteRedirectLocation: i.WebsiteRedirectLocation,
}
if i.ChecksumAlgorithm != "" {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(i.ChecksumAlgorithm)
} else {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(checksumAlgorithm)
}
return input
}
func (i UploadObjectInput) mapCompleteMultipartUploadInput(uploadID *string, completedParts completedParts) *s3.CompleteMultipartUploadInput {
input := &s3.CompleteMultipartUploadInput{
Bucket: i.Bucket,
Key: i.Key,
UploadId: uploadID,
ChecksumCRC32: i.ChecksumCRC32,
ChecksumCRC32C: i.ChecksumCRC32C,
ChecksumCRC64NVME: i.ChecksumCRC64NVME,
ChecksumSHA1: i.ChecksumSHA1,
ChecksumSHA256: i.ChecksumSHA256,
ExpectedBucketOwner: i.ExpectedBucketOwner,
IfMatch: i.IfMatch,
IfNoneMatch: i.IfNoneMatch,
RequestPayer: s3types.RequestPayer(i.RequestPayer),
SSECustomerAlgorithm: i.SSECustomerAlgorithm,
SSECustomerKey: i.SSECustomerKey,
SSECustomerKeyMD5: i.SSECustomerKeyMD5,
}
var parts []s3types.CompletedPart
for _, part := range completedParts {
parts = append(parts, part.MapCompletedPart())
}
if parts != nil {
input.MultipartUpload = &s3types.CompletedMultipartUpload{Parts: parts}
}
return input
}
func (i UploadObjectInput) mapUploadPartInput(body io.Reader, partNum *int32, uploadID *string, checksumAlgorithm types.ChecksumAlgorithm) *s3.UploadPartInput {
input := &s3.UploadPartInput{
Bucket: i.Bucket,
Key: i.Key,
Body: body,
PartNumber: partNum,
UploadId: uploadID,
ExpectedBucketOwner: i.ExpectedBucketOwner,
RequestPayer: s3types.RequestPayer(i.RequestPayer),
SSECustomerAlgorithm: i.SSECustomerAlgorithm,
SSECustomerKey: i.SSECustomerKey,
SSECustomerKeyMD5: i.SSECustomerKeyMD5,
}
if i.ChecksumAlgorithm != "" {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(i.ChecksumAlgorithm)
} else {
input.ChecksumAlgorithm = s3types.ChecksumAlgorithm(checksumAlgorithm)
}
return input
}
func (i *UploadObjectInput) mapAbortMultipartUploadInput(uploadID *string) *s3.AbortMultipartUploadInput {
input := &s3.AbortMultipartUploadInput{
Bucket: i.Bucket,
Key: i.Key,
UploadId: uploadID,
ExpectedBucketOwner: i.ExpectedBucketOwner,
RequestPayer: s3types.RequestPayer(i.RequestPayer),
}
return input
}
// UploadObjectOutput represents a response from the PutObject() call. It contains common fields
// of s3 PutObject and CompleteMultipartUpload output
type UploadObjectOutput struct {
// The bucket where the newly created object is put
Bucket *string
// The object key of the newly created object.
Key *string
// Indicates whether the uploaded object uses an S3 Bucket Key for server-side
// encryption with Amazon Web Services KMS (SSE-KMS).
BucketKeyEnabled *bool
// The base64-encoded, 32-bit CRC32 checksum of the object.
ChecksumCRC32 *string
// The base64-encoded, 32-bit CRC32C checksum of the object.
ChecksumCRC32C *string
// The Base64 encoded, 64-bit CRC64NVME checksum of the object.
ChecksumCRC64NVME *string
// The base64-encoded, 160-bit SHA-1 digest of the object.
ChecksumSHA1 *string
// The base64-encoded, 256-bit SHA-256 digest of the object.
ChecksumSHA256 *string
// This header specifies the checksum type of the object, which determines how
// part-level checksums are combined to create an object-level checksum for
// multipart objects. For PutObject uploads, the checksum type is always
// FULL_OBJECT . You can use this header as a data integrity check to verify that
// the checksum type that is received is the same checksum that was specified. For
// more information, see [Checking object integrity]in the Amazon S3 User Guide.
//
// [Checking object integrity]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
ChecksumType types.ChecksumType
// Entity tag for the uploaded object.
ETag *string
// If the object expiration is configured, this will contain the expiration date
// (expiry-date) and rule ID (rule-id). The value of rule-id is URL encoded.
Expiration *string
// The URI that identifies the newly created object.
Location *string
// The ID for a multipart upload to S3. In the case of an error the error
// can be cast to the MultiUploadFailure interface to extract the upload ID.
// Will be empty string if multipart upload was not used, and the object
// was uploaded as a single PutObject call.
UploadID *string
// The list of parts that were uploaded and their checksums. Will be empty
// if multipart upload was not used, and the object was uploaded as a
// single PutObject call.
CompletedParts []types.CompletedPart
// Total length of the object
ContentLength *int64
// If present, indicates that the requester was successfully charged for the
// request.
RequestCharged types.RequestCharged
// If server-side encryption with a customer-provided encryption key was
// requested, the response will include this header to confirm the encryption
// algorithm that's used.
//
// This functionality is not supported for directory buckets.
SSECustomerAlgorithm *string
// If server-side encryption with a customer-provided encryption key was
// requested, the response will include this header to provide the round-trip
// message integrity verification of the customer-provided encryption key.
//
// This functionality is not supported for directory buckets.
SSECustomerKeyMD5 *string
// If present, indicates the Amazon Web Services KMS Encryption Context to use for
// object encryption. The value of this header is a Base64 encoded string of a
// UTF-8 encoded JSON, which contains the encryption context as key-value pairs.
// This value is stored as object metadata and automatically gets passed on to
// Amazon Web Services KMS for future GetObject operations on this object.
SSEKMSEncryptionContext *string
// If present, specifies the ID of the Amazon Web Services Key Management Service
// (Amazon Web Services KMS) symmetric customer managed customer master key (CMK)
// that was used for the object.
SSEKMSKeyID *string
// If you specified server-side encryption either with an Amazon S3-managed
// encryption key or an Amazon Web Services KMS customer master key (CMK) in your
// initiate multipart upload request, the response includes this header. It
// confirms the encryption algorithm that Amazon S3 used to encrypt the object.
ServerSideEncryption types.ServerSideEncryption
// The size of the object in bytes. This value is only be present if you append
// to an object.
//
// This functionality is only supported for objects in the Amazon S3 Express One
// Zone storage class in directory buckets.
Size *int64
// The version of the object that was uploaded. Will only be populated if
// the S3 Bucket is versioned. If the bucket is not versioned this field
// will not be set.
VersionID *string
// Metadata pertaining to the operation's result.
ResultMetadata smithymiddleware.Metadata
}
func (o *UploadObjectOutput) mapFromPutObjectOutput(out *s3.PutObjectOutput, bucket, key *string, contentLength int64) {
o.Bucket = bucket
o.Key = key
o.BucketKeyEnabled = out.BucketKeyEnabled
o.ChecksumCRC32 = out.ChecksumCRC32
o.ChecksumCRC32C = out.ChecksumCRC32C
o.ChecksumCRC64NVME = out.ChecksumCRC64NVME
o.ChecksumSHA1 = out.ChecksumSHA1
o.ChecksumSHA256 = out.ChecksumSHA256
o.ChecksumType = types.ChecksumType(out.ChecksumType)
o.ContentLength = aws.Int64(contentLength)
o.ETag = out.ETag
o.Expiration = out.Expiration
o.RequestCharged = types.RequestCharged(out.RequestCharged)
o.SSECustomerAlgorithm = out.SSECustomerAlgorithm
o.SSECustomerKeyMD5 = out.SSECustomerKeyMD5
o.SSEKMSEncryptionContext = out.SSEKMSEncryptionContext
o.SSEKMSKeyID = out.SSEKMSKeyId
o.ServerSideEncryption = types.ServerSideEncryption(out.ServerSideEncryption)
o.Size = out.Size
o.VersionID = out.VersionId
o.ResultMetadata = out.ResultMetadata
}
func (o *UploadObjectOutput) mapFromCompleteMultipartUploadOutput(out *s3.CompleteMultipartUploadOutput, bucket, uploadID *string, contentLength int64, completedParts completedParts) {
o.Bucket = bucket
o.Key = out.Key
o.UploadID = uploadID
o.CompletedParts = completedParts
o.BucketKeyEnabled = out.BucketKeyEnabled
o.ChecksumCRC32 = out.ChecksumCRC32
o.ChecksumCRC32C = out.ChecksumCRC32C
o.ChecksumCRC64NVME = out.ChecksumCRC64NVME
o.ChecksumSHA1 = out.ChecksumSHA1
o.ChecksumSHA256 = out.ChecksumSHA256
o.ChecksumType = types.ChecksumType(out.ChecksumType)
o.ContentLength = aws.Int64(contentLength)
o.ETag = out.ETag
o.Expiration = out.Expiration
o.Location = out.Location
o.RequestCharged = types.RequestCharged(out.RequestCharged)
o.SSEKMSKeyID = out.SSEKMSKeyId
o.ServerSideEncryption = types.ServerSideEncryption(out.ServerSideEncryption)
o.VersionID = out.VersionId
o.ResultMetadata = out.ResultMetadata
}
// UploadObject uploads an object to S3, intelligently buffering large
// files into smaller chunks and sending them in parallel across multiple
// goroutines. You can configure the chunk size and concurrency through the
// Options parameters.
//
// Additional functional options can be provided to configure the individual
// upload. These options are copies of the original Options instance, the client of which UploadObject is called from.
// Modifying the options will not impact the original Client and Options instance.
func (c *Client) UploadObject(ctx context.Context, input *UploadObjectInput, opts ...func(*Options)) (*UploadObjectOutput, error) {
i := uploader{in: input, options: c.options.Copy()}
for _, opt := range opts {
opt(&i.options)
}
return i.upload(ctx)
}
type uploader struct {
options Options
in *UploadObjectInput
// PartPool allows for the re-usage of streaming payload part buffers between upload calls
partPool bytesBufferPool
objectSize int64
multipleRead bool
progressEmitter *singleObjectProgressEmitter
}
func (u *uploader) upload(ctx context.Context) (*UploadObjectOutput, error) {
if err := u.init(); err != nil {
return nil, fmt.Errorf("unable to initialize upload: %w", err)
}
clientOptions := []func(o *s3.Options){
func(o *s3.Options) {
o.APIOptions = append(o.APIOptions,
middleware.AddSDKAgentKey(middleware.FeatureMetadata, userAgentKey),
addFeatureUserAgent,
)
}}
r, n, cleanUp, err := u.nextReader(ctx)
if err == io.EOF {
return u.singleUpload(ctx, r, n, cleanUp, clientOptions...)
} else if err != nil {
cleanUp()
return nil, err
}
u.partPool = newDefaultSlicePool(u.options.PartSizeBytes, u.options.Concurrency+1) // only create the caching pool for multipart upload
defer u.partPool.Close()
mu := multiUploader{
uploader: u,
}
return mu.upload(ctx, r, n, cleanUp, clientOptions...)
}
func (u *uploader) init() error {
u.progressEmitter = &singleObjectProgressEmitter{
Listeners: u.options.ObjectProgressListeners,
}
if err := u.initSize(); err != nil {
return err
}
return nil
}
// initSize checks user configured partsize and up-size it if calculated part count exceeds max value
func (u *uploader) initSize() error {
u.objectSize = -1
switch r := u.in.Body.(type) {
case io.Seeker:
n, err := types.SeekerLen(r)
if err != nil {
return err
}
u.objectSize = n
default:
if l := aws.ToInt64(u.in.ContentLength); l > 0 {
u.objectSize = l
}
}
// Try to adjust partSize if it is too small and account for
// integer division truncation.
if u.objectSize/u.options.PartSizeBytes >= int64(defaultMaxUploadParts) {
// Add one to the part size to account for remainders
// during the size calculation. e.g odd number of bytes.
u.options.PartSizeBytes = (u.objectSize / int64(defaultMaxUploadParts)) + 1
}
return nil
}
func (u *uploader) singleUpload(ctx context.Context, r io.Reader, sz int, cleanUp func(), clientOptions ...func(*s3.Options)) (*UploadObjectOutput, error) {
defer cleanUp()
params := u.in.mapSingleUploadInput(r, u.options.ChecksumAlgorithm)
objectSize := int64(sz)
u.progressEmitter.Start(ctx, u.in, objectSize)
out, err := u.options.S3.PutObject(ctx, params, clientOptions...)
if err != nil {
u.progressEmitter.Failed(ctx, err)
return nil, err
}
var output UploadObjectOutput
output.mapFromPutObjectOutput(out, u.in.Bucket, u.in.Key, objectSize)
u.progressEmitter.BytesTransferred(ctx, objectSize)
u.progressEmitter.Complete(ctx, &output)
return &output, nil
}
// nextReader reads the next chunk of data from input Body
func (u *uploader) nextReader(ctx context.Context) (io.Reader, int, func(), error) {
if !u.multipleRead {
u.multipleRead = true
// read first part up to a maximum of PartSize to avoid allocating 8MB buffer out of the gate
r := io.LimitReader(u.in.Body, u.options.PartSizeBytes)
firstPart, err := io.ReadAll(r)
if err != nil {
return nil, 0, func() {}, err
}
n := len(firstPart)
// Use the minimum of MultipartUploadThreshold and PartSizeBytes as the cutoff
// for single vs multipart upload. We can only observe up to PartSizeBytes of
// data here, so the threshold is capped to avoid silent data truncation.
threshold := u.options.MultipartUploadThreshold
if u.options.PartSizeBytes < threshold {
threshold = u.options.PartSizeBytes
}
if int64(n) < threshold {
return bytes.NewReader(firstPart), n, func() {}, io.EOF
}
return bytes.NewReader(firstPart), n, func() {}, nil
}
part, err := u.partPool.Get(ctx)
if err != nil {
return nil, 0, func() {}, err
}
n, err := readFillBuf(u.in.Body, part)
cleanup := func() {
u.partPool.Put(part)
}
return bytes.NewReader(part[0:n]), n, cleanup, err
}
func readFillBuf(r io.Reader, b []byte) (offset int, err error) {
for offset < len(b) && err == nil {
var n int
n, err = r.Read(b[offset:])
offset += n
}
return offset, err
}
type multiUploader struct {
*uploader
wg sync.WaitGroup
m sync.Mutex
err error
uploadID *string
parts completedParts
}
type ulChunk struct {
buf io.Reader
buflen int64
partNum *int32
cleanup func()
}
type completedParts []types.CompletedPart
func (cp completedParts) Len() int {
return len(cp)
}
func (cp completedParts) Less(i, j int) bool {
return aws.ToInt32(cp[i].PartNumber) < aws.ToInt32(cp[j].PartNumber)
}
func (cp completedParts) Swap(i, j int) {
cp[i], cp[j] = cp[j], cp[i]
}
// upload will perform a multipart upload using the firstBuf buffer containing
// the first chunk of data.
func (u *multiUploader) upload(ctx context.Context, firstBuf io.Reader, firstBuflen int, cleanup func(), clientOptions ...func(*s3.Options)) (*UploadObjectOutput, error) {
params := u.uploader.in.mapCreateMultipartUploadInput(u.options.ChecksumAlgorithm)
// Create a multipart
u.progressEmitter.Start(ctx, u.in, u.objectSize)
resp, err := u.uploader.options.S3.CreateMultipartUpload(ctx, params, clientOptions...)
if err != nil {
cleanup()
u.progressEmitter.Failed(ctx, err)
return nil, err
}
u.uploadID = resp.UploadId
ch := make(chan ulChunk, u.options.Concurrency)
for i := 0; i < u.options.Concurrency; i++ {
// launch workers
u.wg.Add(1)
go u.readChunk(ctx, ch, clientOptions...)
}
var partNum int32 = 1
ch <- ulChunk{
buf: firstBuf,
buflen: int64(firstBuflen),
partNum: aws.Int32(partNum),
cleanup: cleanup,
}
for u.geterr() == nil && err == nil {
partNum++
var (
data io.Reader
nextChunkLen int