4
votes

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

2

2 Answers

5
votes

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 = Aws::S3::Client.new()
resp = client.get_bucket_location(bucket: bucket_name)
s3_region = resp.data.location_constraint

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

1
votes

For aws-sdk (2.6.5), it is:

client.get_bucket_location(bucket: bucket_name).location_constraint

But then, now, how we get a list of buckets that belong to a specific region?