Skip to content

Commit

Permalink
fix-issue-6297
Browse files Browse the repository at this point in the history
Signed-off-by: Lyndon-Li <[email protected]>
  • Loading branch information
Lyndon-Li committed May 31, 2023
1 parent 4322ae1 commit 6fcc2c5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/repository/config/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"os"

goerr "errors"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -80,16 +82,20 @@ func GetS3Credentials(config map[string]string) (*credentials.Value, error) {
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error
// if the region cannot be determined.
func GetAWSBucketRegion(bucket string) (string, error) {
var region string

sess, err := session.NewSession()
if err != nil {
return "", errors.WithStack(err)
}

var region string
var requestErrs []error

for _, partition := range endpoints.DefaultPartitions() {
for regionHint := range partition.Regions() {
region, _ = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint)
region, err = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint)
if err != nil {
requestErrs = append(requestErrs, err)
}

// we only need to try a single region hint per partition, so break after the first
break
Expand All @@ -100,5 +106,9 @@ func GetAWSBucketRegion(bucket string) (string, error) {
}
}

return "", errors.New("unable to determine bucket's region")
if requestErrs == nil {
return "", errors.New("unable to determine bucket's region")
} else {
return "", goerr.Join(requestErrs...)
}
}

0 comments on commit 6fcc2c5

Please sign in to comment.