创建 AWS S3 存储桶时,无区域是什么意思? [英] what does region None mean when creating a AWS S3 Bucket?
问题描述
我刚刚使用 AWS CLI 创建了一个存储桶:
I have just created a bucket with AWS CLI:
aws s3 mb s3://new-bucket-created
然后在使用 boto3 列出我的 S3 帐户中的所有存储桶时:
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))
输出显示区域指定为 sa-east-1 的所有旧桶,但较新的是无:
the output shows all older buckets with region specified as sa-east-1 but the newer is None:
输出
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?
推荐答案
区域 us-east-1 中的存储桶的 LocationConstraint 为 null .
Buckets in Region us-east-1 have a LocationConstraint of null .
要解决此问题,请更新您的代码
To fix this, update your code
bucket_region = s3.get_bucket_location(Bucket=bucket_name).get('LocationConstraint', 'us-east-1')
(假设 LocationConstraint
键没有实际上存在于响应中)
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)
或buckets.update({bucket_name: bucket_region or 'us-east-1'})
,如果定义了,将返回区域,否则返回默认值
Or buckets.update({bucket_name: bucket_region or 'us-east-1'})
, which will return the region if defined, else fallsthrough to the default value
这篇关于创建 AWS S3 存储桶时,无区域是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!