【发布时间】:2019-11-02 06:35:26
【问题描述】:
是否可以使用 boto API 在 s3 中获取存储桶位置?
我说的是来自 AWS API 的这个函数 - http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
提前致谢。
【问题讨论】:
标签: python amazon-web-services boto
是否可以使用 boto API 在 s3 中获取存储桶位置?
我说的是来自 AWS API 的这个函数 - http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
提前致谢。
【问题讨论】:
标签: python amazon-web-services boto
对于 boto3,您可以使用 get_bucket_location 方法,该方法将返回给您以下之一(截至 2019 年 11 月):
'EU'|'eu-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-southeast-1'|'ap-东南-2'|'ap-northeast-1'|'sa-east-1'|'cn-north-1'|'eu-central-1'
示例方法如下所示:
def get_location(client, bucket_name):
response = client.get_bucket_location(Bucket=bucket_name)
return response['LocationConstraint']
【讨论】:
None 返回表示存储桶不存在,或者您没有查询存储桶位置所需的权限。我需要看到更多的回复才能确定。
你可以调用get_location()方法:
conn = boto.connect_s3()
bucket = conn.get_bucket(bucket_name)
bucket_location = bucket.get_location()
if bucket_location:
conn = boto.s3.connect_to_region(bucket_location)
bucket = conn.get_bucket(bucket_name)
http://boto.cloudhackers.com/en/latest/ref/s3.html#boto.s3.bucket.Bucket.get_location
【讨论】:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetBucketLocation" ], "Resource": "arn:aws:s3:::*" } }
if bucket_location: 后面的代码有什么意义?如果你可以在第 2 行获取存储桶,在第 3 行获取存储桶的位置,为什么还要再次调用conn.get_bucket(bucket_name)?