【问题标题】:Boto get s3 bucket locationBoto 获取 s3 存储桶位置
【发布时间】: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


    【解决方案1】:

    对于 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']
    

    请参阅documentation for more info

    【讨论】:

    • 如果返回 None 意味着什么?
    • None 返回表示存储桶不存在,或者您没有查询存储桶位置所需的权限。我需要看到更多的回复才能确定。
    • 感谢@eatsfood,刚刚在post 中找到了我想要的东西。
    【解决方案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

    【讨论】:

    • 对于不同的存储桶,我有 2 种不同的行为:1. 它只返回空字符串 2. 它返回 403: boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
    • 确保您的 IAM 用户/角色有权调用 GetBucketLocation API。这是对 Global S3 服务的 API 调用,而不仅仅是特定的存储桶; { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetBucketLocation" ], "Resource": "arn:aws:s3:::*" } }
    • boto 使用签名类型 v2,默认情况下会导致 403 问题吗?我添加了 s3:GetBucketLocation 但仍然收到 403。
    • 您将得到一个空字符串(或使用 boto3 的 LocationConstraint=None),用于美国标准。如果您无权针对特定存储桶调用 s3:GetBucketLocation(是所有存储桶,还是仅部分存储桶?),您将看到 403
    • 第 4 行 if bucket_location: 后面的代码有什么意义?如果你可以在第 2 行获取存储桶,在第 3 行获取存储桶的位置,为什么还要再次调用conn.get_bucket(bucket_name)
    猜你喜欢
    • 2015-07-31
    • 2019-08-07
    • 2018-08-25
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多