2
votes

I have just created a bucket with AWS CLI:

aws s3 mb s3://new-bucket-created

And then when listing all buckets in my S3 account with boto3:

s3 = boto3.client('s3')
response = s3.list_buckets()

buckets = {}
for bucket in response['Buckets']:
    bucket_name = bucket["Name"]
    bucket_region = s3.get_bucket_location(Bucket=bucket_name)['LocationConstraint']
    buckets.update({bucket_name:bucket_region})

for bucket, region in buckets.items():
    print("{}, {}".format(bucket, region))

the output shows all older buckets with region specified as sa-east-1 but the newer is None:

Output

older-bucket, sa-east-1
another-older-bucket, sa-east-1
yet-another-older-bucket, sa-east-1
new-bucket-created, None

I can write in that new-bucket-created, but why can't i see its region? Sure there's a place, any thoughts?

1
Did you read the docs for when s3.get_bucket_location might return None? - OneCricketeer

1 Answers

2
votes

Buckets in Region us-east-1 have a LocationConstraint of null .

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.get_bucket_location

To fix this, update your code

bucket_region = s3.get_bucket_location(Bucket=bucket_name).get('LocationConstraint', 'us-east-1') (this assumes LocationConstraint key doesn't actually exist in the response)

Or buckets.update({bucket_name: bucket_region or 'us-east-1'}), which will return the region if defined, else fallsthrough to the default value