Currently, I am not aware of straight-forward method to learn about what region a bucket is located at.
import boto3
s3 = boto3.resource("s3")
bucket_name = "my_bucket_name"
bucket = s3.Bucket(bucket_name)
So far so good, now I am going to learn about region the bucket is living at:
bucket.region_name # not available
instead, I have to use following call (notice, how many times I have to say "bucket" or "location"):
region_name = s3.meta.client.get_bucket_location(Bucket=bucket.name)["LocationConstraint"]
It would be great to have bucket property "region_name" available.
Thanks for the suggestion. The current behavior is consistent across all SDKs. We will need to see how feasible it is to change that.
@rayluo the AWS API has a location call available: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
Why would you NOT offer that through boto3? (Just because everyone else is doing it doesn't make it right).
Not only that, @vlcinsky , but you need additional logic because the LocationConstraint is "None" when the bucket is in US Standard. ugh.
I know this issue is old but there is still no solutions for this. I found a way without extra-logic needed.
import boto3
client = boto3.client('s3') # the region_name does not matter
client.head_bucket(Bucket='bucket')['ResponseMetadata']['HTTPHeaders']['x-amz-bucket-region']
=> 'us-east-1'
What do you think? Is it dangerous to rely on metadata?
Most helpful comment
I know this issue is old but there is still no solutions for this. I found a way without extra-logic needed.
What do you think? Is it dangerous to rely on metadata?