diff --git a/command/sync.go b/command/sync.go index 6dede5a66..22a274586 100644 --- a/command/sync.go +++ b/command/sync.go @@ -301,12 +301,22 @@ func compareObjects(sourceObjects, destObjects chan *storage.Object, isSrcBatch // given URLs. The returned channels gives objects sorted in ascending order // with respect to their url.Relative path. See also storage.Less. func (s Sync) getSourceAndDestinationObjects(ctx context.Context, cancel context.CancelFunc, srcurl, dsturl *url.URL) (chan *storage.Object, chan *storage.Object, error) { - sourceClient, err := storage.NewClient(ctx, srcurl, s.storageOpts) + // Create source client with source region + srcOpts := s.storageOpts + if s.srcRegion != "" { + srcOpts.SetRegion(s.srcRegion) + } + sourceClient, err := storage.NewClient(ctx, srcurl, srcOpts) if err != nil { return nil, nil, err } - destClient, err := storage.NewClient(ctx, dsturl, s.storageOpts) + // Create destination client with destination region + dstOpts := s.storageOpts + if s.dstRegion != "" { + dstOpts.SetRegion(s.dstRegion) + } + destClient, err := storage.NewClient(ctx, dsturl, dstOpts) if err != nil { return nil, nil, err } diff --git a/storage/s3.go b/storage/s3.go index 3313be66e..ff49998ff 100644 --- a/storage/s3.go +++ b/storage/s3.go @@ -1334,20 +1334,18 @@ func (sc *SessionCache) clear() { } func setSessionRegion(ctx context.Context, sess *session.Session, bucket string) error { - region := aws.StringValue(sess.Config.Region) - - if region != "" { - return nil - } - - // set default region - sess.Config.Region = aws.String(endpoints.UsEast1RegionID) - + // If no bucket specified, use default or configured region if bucket == "" { + region := aws.StringValue(sess.Config.Region) + if region == "" { + // set default region for bucket-less operations + sess.Config.Region = aws.String(endpoints.UsEast1RegionID) + } return nil } - // auto-detection + // Always auto-detect bucket region when bucket is specified + // This makes behavior consistent with AWS CLI region, err := s3manager.GetBucketRegion(ctx, sess, bucket, "", func(r *request.Request) { // s3manager.GetBucketRegion uses Path style addressing and // AnonymousCredentials by default, updating Request's Config to match @@ -1364,6 +1362,12 @@ func setSessionRegion(ctx context.Context, sess *session.Session, bucket string) err = fmt.Errorf("session: fetching region failed: %v", err) msg := log.ErrorMessage{Err: err.Error()} log.Error(msg) + + // Fall back to configured region if auto-detection fails + configuredRegion := aws.StringValue(sess.Config.Region) + if configuredRegion == "" { + sess.Config.Region = aws.String(endpoints.UsEast1RegionID) + } } else { sess.Config.Region = aws.String(region) }