【问题标题】:what does region None mean when creating a AWS S3 Bucket?创建 AWS S3 存储桶时,区域 None 是什么意思?
【发布时间】:2021-05-03 22:47:55
【问题描述】:

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

我可以在新创建的存储桶中写入,但为什么我看不到它的区域?确定有地方,有什么想法吗?

【问题讨论】:

  • 您是否阅读过有关 s3.get_bucket_location 何时可能返回 None 的文档?

标签: python amazon-web-services boto3


【解决方案1】:

区域 us-east-1 中的存储桶的 LocationConstraint 为 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'}),如果定义了则返回区域,否则返回默认值

【讨论】:

  • 作为唯一返回 None 的区域没有多大意义,但它就是这样工作的,很好,谢谢!
  • @LuisVenezian 这是一个向后兼容的东西,其中 S3 在任何其他 AWS 区域存在之前就已经存在,因此在 us-east-1 中有 S3 存储桶是在指定区域时创建的一个要求。
猜你喜欢
  • 1970-01-01
  • 2021-03-24
  • 2020-05-26
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
相关资源
最近更新 更多