azelo azelo - 9 months ago 98
Ruby Question

Determine S3 Bucket Region

I'm using the AWS Ruby SDK v2 to access various buckets across a couple of regions. Is it possible to determine the region of (at runtime) of each bucket before I access it, so I can avoid the error below, which I get if I configure the AWS S3 client with the wrong region?

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

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

aws s3api get-bucket-location


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

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

This one is calling the same API as aws s3api get-bucket-location