Skip to content

Commit

Permalink
s3: use different strategy to resolve s3 region
Browse files Browse the repository at this point in the history
The API endpoint GetBucketLocation requires
top level permission.

If we do an authenticated head request to a bucket, the bucket location will be returned in the HTTP headers.

See aws/aws-cli#2431 (comment)
  • Loading branch information
panthony committed Oct 29, 2022
1 parent 22abcc9 commit 94231a6
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions backend/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2992,19 +2992,26 @@ func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {

// Gets the bucket location
func (f *Fs) getBucketLocation(ctx context.Context, bucket string) (string, error) {
req := s3.GetBucketLocationInput{
req, _ := f.c.HeadBucketRequest(&s3.HeadBucketInput{
Bucket: &bucket,
}
var resp *s3.GetBucketLocationOutput
var err error
err = f.pacer.Call(func() (bool, error) {
resp, err = f.c.GetBucketLocation(&req)
return f.shouldRetry(ctx, err)
})
if err != nil {
return "", err
req.SetContext(ctx)
req.Config.Credentials = credentials.AnonymousCredentials

// Disable HTTP redirects to prevent an invalid 301 from eating the response
// because Go's HTTP client will fail, and drop the response if an 301 is
// received without a location header. S3 will return a 301 without the
// location header for HeadObject API calls.
req.DisableFollowRedirects = true

// an error (HTTP 307, 403, ...) is expected here but we are interested in HTTP headers
_ = req.Send()

if region := req.HTTPResponse.Header.Get("x-amz-bucket-region"); len(region) != 0 {
return s3.NormalizeBucketLocation(aws.StringValue(&region)), nil
}
return s3.NormalizeBucketLocation(aws.StringValue(resp.LocationConstraint)), nil

return "", fmt.Errorf("missing 'x-amz-bucket-region' http header")
}

// Updates the region for the bucket by reading the region from the
Expand Down

0 comments on commit 94231a6

Please sign in to comment.