Boto获取s3 bucket位置

2024-05-02 14:44:50 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用boto API在s3中获取bucket位置?

我说的是AWS API中的这个函数-http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html

提前谢谢。


Tags: 函数comawsapihttpdocsamazons3
2条回答

可以调用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

对于boto3,您可以使用get_bucket_location方法,该方法将返回给您以下之一(截至2019年11月):

'EU'|'EU-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-south-1'|'ap-south-2'|'ap-东北-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']

请参阅documentation for more info

相关问题 更多 >