@@ -47,10 +47,11 @@ type Client interface {
4747 ListParts (ctx context.Context , key , uploadID * string ) (parts map [int ]* Part )
4848 GetUploadID (ctx context.Context , key * string ) (uploadID * string )
4949 ListSelectedPrefixes (ctx context.Context , key * string ) (prefixes []* string )
50+ ListSelectedPrefixesFromThirdBucket (ctx context.Context , bucket * string , key * string ) (prefixes []* string )
5051
5152 // WRITE
52- PutObject (ctx context.Context , key * string , body []byte , storageClass , acl * string , meta * Metadata ) (etag * string , err error )
53- CreateMultipartUpload (ctx context.Context , key , storageClass , acl * string , meta * Metadata ) (uploadID * string , err error )
53+ PutObject (ctx context.Context , key * string , body []byte , storageClass , acl * string , sseType * string , sseKMSKeyId * string , meta * Metadata ) (etag * string , err error )
54+ CreateMultipartUpload (ctx context.Context , key , storageClass , acl * string , sseType * string , sseKMSKeyId * string , meta * Metadata ) (uploadID * string , err error )
5455 CompleteMultipartUpload (ctx context.Context , key , uploadID * string , parts []* Part ) (etag * string , err error )
5556 UploadPart (ctx context.Context , key * string , body []byte , uploadID * string , partNumber int ) (etag * string , err error )
5657 AbortMultipartUpload (ctx context.Context , key , uploadID * string ) (err error )
@@ -166,6 +167,19 @@ func NewS3Client(ctx context.Context, bucket, prefix, prefixList, endpoint, regi
166167
167168}
168169
170+ // NewS3ClientWithEC2Role creates a S3Client instance which uses EC2 Role to access S3
171+ func NewS3ClientWithEC2Role (ctx context.Context , bucket , prefixList string ) * S3Client {
172+ cfg := loadDefaultConfig (ctx )
173+
174+ client := s3 .NewFromConfig (cfg )
175+
176+ return & S3Client {
177+ bucket : bucket ,
178+ prefixList : prefixList ,
179+ client : client ,
180+ }
181+ }
182+
169183// GetObject is a function to get (download) object from Amazon S3
170184func (c * S3Client ) GetObject (ctx context.Context , key * string , size , start , chunkSize int64 , version string ) ([]byte , error ) {
171185 // log.Printf("S3> Downloading %s with %d bytes start from %d\n", key, size, start)
@@ -459,8 +473,45 @@ func (c *S3Client) ListSelectedPrefixes(ctx context.Context, key *string) (prefi
459473 return
460474}
461475
476+ // ListSelectedPrefixesFromThirdBucket is a function to list prefixes from a list file in a specific bucket.
477+ func (c * S3Client ) ListSelectedPrefixesFromThirdBucket (ctx context.Context , bucket * string , key * string ) (prefixes []* string ) {
478+ downloader := manager .NewDownloader (c .client )
479+ getBuf := manager .NewWriteAtBuffer ([]byte {})
480+
481+ input := & s3.GetObjectInput {
482+ Bucket : bucket ,
483+ Key : key ,
484+ }
485+
486+ downloadStart := time .Now ()
487+ log .Printf ("Start downloading the Prefix List File from bucket: %s" , * bucket )
488+ _ , err := downloader .Download (ctx , getBuf , input )
489+ downloadEnd := time .Since (downloadStart )
490+ if err != nil {
491+ log .Printf ("Error downloading the Prefix List File: %s" , err )
492+ return nil
493+ } else {
494+ log .Printf ("Download the Prefix List File Completed in %v\n " , downloadEnd )
495+ }
496+
497+ start := time .Now ()
498+ prefixesValue := make ([]string , 0 )
499+
500+ for i , line := range strings .Split (string (getBuf .Bytes ()), "\n " ) {
501+ if len (line ) > 0 {
502+ prefixesValue = append (prefixesValue , line )
503+ prefixes = append (prefixes , & prefixesValue [i ])
504+ }
505+ }
506+
507+ end := time .Since (start )
508+ log .Printf ("Got %d prefixes from the customized list file in %v" , len (prefixes ), end )
509+ return
510+ }
511+
512+
462513// PutObject is a function to put (upload) an object to Amazon S3
463- func (c * S3Client ) PutObject (ctx context.Context , key * string , body []byte , storageClass , acl * string , meta * Metadata ) (etag * string , err error ) {
514+ func (c * S3Client ) PutObject (ctx context.Context , key * string , body []byte , storageClass , acl * string , sseType * string , sseKMSKeyId * string , meta * Metadata ) (etag * string , err error ) {
464515 // log.Printf("S3> Uploading object %s to bucket %s\n", key, c.bucket)
465516
466517 md5Bytes := md5 .Sum (body )
@@ -481,6 +532,14 @@ func (c *S3Client) PutObject(ctx context.Context, key *string, body []byte, stor
481532 StorageClass : types .StorageClass (* storageClass ),
482533 ACL : types .ObjectCannedACL (* acl ),
483534 }
535+ switch * sseType {
536+ case "AES256" :
537+ input .ServerSideEncryption = types .ServerSideEncryptionAes256
538+ case "AWS_KMS" :
539+ input .ServerSideEncryption = types .ServerSideEncryptionAwsKms
540+ input .SSEKMSKeyId = sseKMSKeyId
541+ }
542+
484543 if meta != nil {
485544 input .ContentType = meta .ContentType
486545 input .ContentEncoding = meta .ContentEncoding
@@ -522,7 +581,7 @@ func (c *S3Client) DeleteObject(ctx context.Context, key *string) (err error) {
522581// CreateMultipartUpload is a function to initilize a multipart upload process.
523582// This func returns an upload ID used to indicate the multipart upload.
524583// All parts will be uploaded with this upload ID, after that, all parts by this ID will be combined to create the full object.
525- func (c * S3Client ) CreateMultipartUpload (ctx context.Context , key , storageClass , acl * string , meta * Metadata ) (uploadID * string , err error ) {
584+ func (c * S3Client ) CreateMultipartUpload (ctx context.Context , key , storageClass , acl * string , sseType * string , sseKMSKeyId * string , meta * Metadata ) (uploadID * string , err error ) {
526585 // log.Printf("S3> Create Multipart Upload for %s\n", *key)
527586 if * acl == "" {
528587 * acl = string (types .ObjectCannedACLBucketOwnerFullControl )
@@ -534,6 +593,13 @@ func (c *S3Client) CreateMultipartUpload(ctx context.Context, key, storageClass,
534593 StorageClass : types .StorageClass (* storageClass ),
535594 ACL : types .ObjectCannedACL (* acl ),
536595 }
596+ switch * sseType {
597+ case "AES256" :
598+ input .ServerSideEncryption = types .ServerSideEncryptionAes256
599+ case "AWS_KMS" :
600+ input .ServerSideEncryption = types .ServerSideEncryptionAwsKms
601+ input .SSEKMSKeyId = sseKMSKeyId
602+ }
537603 if meta != nil {
538604 input .ContentType = meta .ContentType
539605 input .ContentEncoding = meta .ContentEncoding
0 commit comments