Boto Get S3 Bucket Location
Is It Possible to Get Bucket Location in S3 Using Boto Api? I'm Talking About This Function from Aws Api - Thanks in Advance. 2 Answers for Boto3, You Can Use...
Is it possible to get bucket location in s3 using boto API ?
I'm talking about this function from AWS API -
Thanks in advance.
2 Answers
For boto3, you can use the get_bucket_location method, which will return to you one of the following (as of November 2019):
'EU'|'eu-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'sa-east-1'|'cn-north-1'|'eu-central-1'
A sample method would be something like this:
def get_location(client, bucket_name):
response = client.get_bucket_location(Bucket=bucket_name)
return response['LocationConstraint']
See the documentation for more info.
You can call the get_location() method:
conn = boto.connect_s3()
bucket = conn.get_bucket(bucket_name)
bucket_location = bucket.get_location()
if bucket_location:
conn = boto.s3.connect_to_region(bucket_location)
bucket = conn.get_bucket(bucket_name)