gpt4 book ai didi

python - 创建 AWS S3 存储桶时,无区域是什么意思?

转载 作者:行者123 更新时间:2023-12-04 07:42:44 28 4
gpt4 key购买 nike

我刚刚使用 AWS CLI 创建了一个存储桶:

aws s3 mb s3://new-bucket-created
然后在使用 boto3 列出我的 S3 帐户中的所有存储桶时:
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 的所有旧桶,但较新的是无:
输出
older-bucket, sa-east-1
another-older-bucket, sa-east-1
yet-another-older-bucket, sa-east-1
new-bucket-created, None
我可以在那个新创建的存储桶中写入,但为什么我看不到它的区域?肯定有一个地方,有什么想法吗?

最佳答案

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
要解决此问题,请更新您的代码 bucket_region = s3.get_bucket_location(Bucket=bucket_name).get('LocationConstraint', 'us-east-1') (这假设 LocationConstraint 键实际上不存在于响应中)
buckets.update({bucket_name: bucket_region or 'us-east-1'}) , 如果定义,将返回区域,否则返回默认值

关于python - 创建 AWS S3 存储桶时,无区域是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67370746/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com