如何跨区域迭代账户中的所有S3 Bucket

How to iterate all S3 Buckets in an account across regions

我正在尝试迭代和检索有关帐户中所有 S3 存储桶的策略。

我正在尝试以下...

s3 = boto3.client('s3')
buckets = s3.list_buckets()
for bucket_name in buckets['Buckets']:
    s3.get_bucket_policy(Bucket=bucket_name['Name'])

当我 运行 这样做时,出现以下错误:

 botocore.exceptions.ClientError: An error occurred (PermanentRedirect) when calling the GetBucketTagging operation: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

我已经尝试按照 https://github.com/boto/boto3/issues/81 使用 s3.meta.client.get_bucket_location(Bucket=bucket_name['Name']),但我得到 'LocationConstraint' 的值 None

我如何知道或确定任意存储桶的正确端点 and/or 区域?

您做对了,减去一个花絮:值 None 表示美国标准区域 [1], or us-east-1. Also, you do not need to use the meta client. The client you've already created has the get_bucket_location 方法。