@@ -59,6 +59,7 @@ type Client interface {
5959// S3Client is an implementation of Client interface for Amazon S3
6060type S3Client struct {
6161 bucket , prefix , prefixList , region , sourceType string
62+ isSrcClient bool
6263 client * s3.Client
6364}
6465
@@ -85,6 +86,35 @@ func getEndpointURL(region, sourceType string) (url string) {
8586 return url
8687}
8788
89+ func getClientCredentialsModifyFn (isSrc bool , src_cred , dst_cred * S3Credentials ) func (o * s3.Options ) {
90+ if isSrc {
91+ return func (o * s3.Options ) {
92+ if src_cred == nil {
93+ log .Print ("src_cred is nil, no modification" )
94+ return
95+ }
96+ if src_cred .noSignRequest {
97+ o .Credentials = aws.AnonymousCredentials {}
98+ }
99+ if src_cred .accessKey != "" {
100+ o .Credentials = aws .NewCredentialsCache (credentials .NewStaticCredentialsProvider (src_cred .accessKey , src_cred .secretKey , "" ))
101+ }
102+ }
103+ }
104+ return func (o * s3.Options ) {
105+ if dst_cred == nil {
106+ log .Print ("dst_cred is nil, no modification" )
107+ return
108+ }
109+ if dst_cred .noSignRequest {
110+ o .Credentials = aws.AnonymousCredentials {}
111+ }
112+ if dst_cred .accessKey != "" {
113+ o .Credentials = aws .NewCredentialsCache (credentials .NewStaticCredentialsProvider (dst_cred .accessKey , dst_cred .secretKey , "" ))
114+ }
115+ }
116+ }
117+
88118// NewS3Client creates a S3Client instance
89119func NewS3Client (ctx context.Context , bucket , prefix , prefixList , endpoint , region , sourceType string , cred * S3Credentials ) * S3Client {
90120
@@ -147,7 +177,7 @@ func (c *S3Client) GetObject(ctx context.Context, key *string, size, start, chun
147177 Range : & bodyRange ,
148178 }
149179
150- output , err := c .client .GetObject (ctx , input )
180+ output , err := c .client .GetObject (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
151181 if err != nil {
152182 log .Printf ("S3> Unable to download %s with %d bytes start from %d - %s\n " , * key , size , start , err .Error ())
153183 return nil , err
@@ -170,10 +200,10 @@ func (c *S3Client) GetObject(ctx context.Context, key *string, size, start, chun
170200func (c * S3Client ) listObjectFn (ctx context.Context , continuationToken , prefix , delimiter * string , maxKeys int32 ) (* s3.ListObjectsV2Output , error ) {
171201
172202 input := & s3.ListObjectsV2Input {
173- Bucket : & c .bucket ,
174- Prefix : prefix ,
175- MaxKeys : maxKeys ,
176- Delimiter : delimiter ,
203+ Bucket : & c .bucket ,
204+ Prefix : prefix ,
205+ MaxKeys : maxKeys ,
206+ Delimiter : delimiter ,
177207 EncodingType : "url" ,
178208 }
179209
@@ -182,7 +212,7 @@ func (c *S3Client) listObjectFn(ctx context.Context, continuationToken, prefix,
182212 }
183213
184214 // start := time.Now()
185- output , err := c .client .ListObjectsV2 (ctx , input )
215+ output , err := c .client .ListObjectsV2 (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
186216 if err != nil {
187217 log .Printf ("Unable to list objects in /%s - %s\n " , * prefix , err .Error ())
188218 return nil , err
@@ -305,7 +335,7 @@ func (c *S3Client) HeadObject(ctx context.Context, key *string) *Metadata {
305335 Key : key ,
306336 }
307337
308- output , err := c .client .HeadObject (ctx , input )
338+ output , err := c .client .HeadObject (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
309339 if err != nil {
310340 log .Printf ("Failed to head object for %s - %s\n " , * key , err .Error ())
311341 return nil
@@ -324,7 +354,9 @@ func (c *S3Client) HeadObject(ctx context.Context, key *string) *Metadata {
324354// ListSelectedPrefixes is a function to list prefixes from a customized list file.
325355func (c * S3Client ) ListSelectedPrefixes (ctx context.Context , key * string ) (prefixes []* string ) {
326356
327- downloader := manager .NewDownloader (c .client )
357+ downloader := manager .NewDownloader (c .client , func (d * manager.Downloader ) {
358+ d .ClientOptions = []func (o * s3.Options ){getClientCredentialsModifyFn (c .isSrcClient , SRC_CRED , DST_CRED )}
359+ })
328360
329361 getBuf := manager .NewWriteAtBuffer ([]byte {})
330362
@@ -391,7 +423,7 @@ func (c *S3Client) PutObject(ctx context.Context, key *string, body []byte, stor
391423 input .Metadata = meta .Metadata
392424 }
393425
394- output , err := c .client .PutObject (ctx , input )
426+ output , err := c .client .PutObject (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
395427 if err != nil {
396428 log .Printf ("S3> Got an error uploading file - %s\n " , err .Error ())
397429 // return nil, err
@@ -413,7 +445,7 @@ func (c *S3Client) DeleteObject(ctx context.Context, key *string) (err error) {
413445 Bucket : & c .bucket ,
414446 Key : key ,
415447 }
416- _ , err = c .client .DeleteObject (ctx , input )
448+ _ , err = c .client .DeleteObject (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
417449 if err != nil {
418450 log .Printf ("S3> Failed to delete object %s - %s\n " , * key , err .Error ())
419451 return err
@@ -444,7 +476,7 @@ func (c *S3Client) CreateMultipartUpload(ctx context.Context, key, storageClass,
444476 input .Metadata = meta .Metadata
445477 }
446478
447- output , err := c .client .CreateMultipartUpload (ctx , input )
479+ output , err := c .client .CreateMultipartUpload (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
448480 if err != nil {
449481 log .Printf ("S3> Unable to create multipart upload for %s - %s\n " , * key , err .Error ())
450482 } else {
@@ -475,7 +507,7 @@ func (c *S3Client) UploadPart(ctx context.Context, key *string, body []byte, upl
475507 ContentMD5 : & contentMD5 ,
476508 }
477509
478- output , err := c .client .UploadPart (ctx , input )
510+ output , err := c .client .UploadPart (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
479511 if err != nil {
480512 log .Printf ("S3> Failed to upload part for %s - %s\n " , * key , err .Error ())
481513 // return nil, err
@@ -513,7 +545,7 @@ func (c *S3Client) CompleteMultipartUpload(ctx context.Context, key, uploadID *s
513545 MultipartUpload : & types.CompletedMultipartUpload {Parts : completedPart },
514546 }
515547
516- output , err := c .client .CompleteMultipartUpload (ctx , input )
548+ output , err := c .client .CompleteMultipartUpload (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
517549 if err != nil {
518550 log .Printf ("S3> Unable to complete multipart upload for %s - %s\n " , * key , err .Error ())
519551 } else {
@@ -540,7 +572,7 @@ func (c *S3Client) ListParts(ctx context.Context, key, uploadID *string) (parts
540572 parts = make (map [int ]* Part , 10000 )
541573
542574 for {
543- output , err := c .client .ListParts (ctx , input )
575+ output , err := c .client .ListParts (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
544576 if err != nil {
545577 log .Printf ("Failed to list parts for %s - %s\n " , * key , err .Error ())
546578 break
@@ -574,7 +606,7 @@ func (c *S3Client) GetUploadID(ctx context.Context, key *string) (uploadID *stri
574606 // MaxUploads: 1,
575607 }
576608
577- output , err := c .client .ListMultipartUploads (ctx , input )
609+ output , err := c .client .ListMultipartUploads (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
578610 if err != nil {
579611 log .Printf ("S3> Failed to list multipart uploads - %s\n " , err .Error ())
580612 return nil
@@ -600,7 +632,7 @@ func (c *S3Client) AbortMultipartUpload(ctx context.Context, key, uploadID *stri
600632 Key : key ,
601633 UploadId : uploadID ,
602634 }
603- _ , err = c .client .AbortMultipartUpload (ctx , input )
635+ _ , err = c .client .AbortMultipartUpload (ctx , input , getClientCredentialsModifyFn ( c . isSrcClient , SRC_CRED , DST_CRED ) )
604636 if err != nil {
605637 log .Printf ("S3> Failed to abort multipart upload for %s - %s\n " , * key , err .Error ())
606638 return err
0 commit comments