创建 AWS S3 存储桶时,无区域是什么意思? [英] what does region None mean when creating a AWS S3 Bucket?

查看:24
本文介绍了创建 AWS S3 存储桶时,无区域是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 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 .

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')(假设 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆