Define S3 Bucket Area

I am using AWS Ruby SDK v2 to access various buckets across multiple regions. Is it possible to define the scope (at runtime) of each bucket before I get it, so I can avoid the below error I get if I configure an AWS S3 client with the wrong region?

The bucket you are trying to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

I know I can lay out the shell and use the below command and parse the response, but ideally I want to stay in the Ruby SDK.

aws s3api get-bucket-location


source to share

2 answers

I couldn't find an official doc for this, but from the aws-sdk spec you should be able to use the following code to get the scope

client =
resp = client.get_bucket_location(bucket: bucket_name)
s3_region =


This call calls the same API as aws s3api get-bucket-location



For aws-sdk (2.6.5) this:

client.get_bucket_location(bucket: bucket_name).location_constraint

But now, how do we get the list of buckets belonging to a particular region?



All Articles