Skip to content

Commit

Permalink
prefer GetBucketLocation() API for s3-compatible endpoints (#1112)
Browse files Browse the repository at this point in the history
* use get-bucket-location api for non-aws-s3 endpoints

* Update pkg/objectstore/bucket.go

Co-authored-by: Tom Manville <tom@kasten.io>

Co-authored-by: Shikhar Mall <shikhar@kasten.io>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Tom Manville <tom@kasten.io>
  • Loading branch information
4 people committed Oct 14, 2021
1 parent 10f50cf commit 357f0f9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/objectstore/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"path"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -193,6 +194,27 @@ func s3BucketRegion(ctx context.Context, cfg ProviderConfig, sec Secret, bucketN
return "", errors.Wrapf(err, "failed to create session, region = %s", r)
}
svc := s3.New(s)

// s3-compatible stores may not support s3manager.GetBucketRegion() API, so
// prefer to use the get-bucket-location API instead
if cfg.Endpoint != "" {
resp, err := svc.GetBucketLocation(&s3.GetBucketLocationInput{
Bucket: aws.String(bucketName),
})
if err == nil {
if resp.LocationConstraint == nil { // per the AWS SDK doc a nil location means us-east-1
return "us-east-1", nil
}
return *resp.LocationConstraint, nil
}
log.Error().
WithContext(ctx).
WithError(err).
Print("GetBucketLocation() failed, falling back to GetBucketRegion()", field.M{"config": c})
// fallback to GetBucketRegion() API if we fail (could be due to
// access-denied or incorrect policies etc)
}

return s3manager.GetBucketRegionWithClient(ctx, svc, bucketName, func(r *request.Request) {
// GetBucketRegionWithClient() uses credentials.AnonymousCredentials by
// default which fails the api request in AWS China. We override the
Expand Down

0 comments on commit 357f0f9

Please sign in to comment.