Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建AWS S3 Bucket时,“区域无”是什么意思?_Python_Amazon Web Services_Boto3 - Fatal编程技术网

Python 创建AWS S3 Bucket时,“区域无”是什么意思?

Python 创建AWS S3 Bucket时,“区域无”是什么意思?,python,amazon-web-services,boto3,Python,Amazon Web Services,Boto3,我刚刚使用AWS CLI创建了一个bucket: 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

我刚刚使用AWS CLI创建了一个bucket:

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
我可以在新创建的bucket中写入,但为什么我看不到它的区域?当然有地方,有什么想法吗

区域us-east-1中的桶的LocationConstraint为null

要修复此问题,请更新您的代码

bucket\u region=s3.get\u bucket\u location(bucket=bucket\u name)。get('LocationConstraint','us-east-1')
(假设
LocationConstraint
键实际上不存在于响应中)


或者
bucket.update({bucket\u name:bucket\u region或'us-east-1'})
,如果定义了该区域,它将返回该区域,否则将返回默认值

您在
s3.get\u bucket\u location
可能不返回任何内容时是否阅读了文档?作为唯一一个不返回任何内容的区域没有多大意义,但这就是它的工作原理,很好,谢谢@LuisVenezian这是一个向后兼容的东西,其中S3在任何其他AWS区域存在之前就存在,因此在us-east-1中有S3存储桶,它们是在不需要指定区域的时候创建的。