简体   繁体   中英

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 = 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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM