如何使用boto3(或其他Python)列出_RequesterPays_ S3存储桶的内容?

时间:2016-04-22 09:05:02

标签: python-3.x amazon-web-services amazon-s3 boto3

您可以从RequesterPays S3存储桶中通过boto3下载文件,如下所示:

  s3_client.download_file('aws-naip', 'md/2013/1m/rgbir/38077/{}'.format(filename), full_path, {'RequestPayer':'requester'})

我无法弄清楚如何列出存储桶中的对象...当我尝试在存储桶上调用objects.all()时,我收到了身份验证错误。

如何使用boto3枚举RequesterPays存储桶的内容? 请注意,这是一种特殊的存储桶,请求者支付S3费用。

3 个答案:

答案 0 :(得分:0)

boto3开始,我们可以看到有#S3.Client.list_objects方法。这可用于枚举对象:

import boto3
s3_client = boto3.client('s3')
resp = s3_client.list_objects(Bucket='RequesterPays')

# print names of all objects
for obj in resp['Contents']:
    print 'Object Name: %s' % obj['Key']

输出:

Object Name: pic.gif
Object Name: doc.txt
Object Name: page.html

如果您获得401,请确保调用API的IAM用户对存储区具有s3:GetObject权限。

答案 1 :(得分:0)

您必须将RequestPayer kwarg传递给list_objects方法。

另外,根据boto3 docs

  

注意:ListObjectsV2是修订后的List Objects API,我们建议您使用此修订后的API进行新的应用程序开发

将它与分页放在一起看起来像:

import boto3
s3_client = boto3.client('s3')

def get_keys(bucket, prefix, requester_pays=False):
    """Get s3 objects from a bucket/prefix
    optionally use requester-pays header
    """
    extra_kwargs = {}
    if requester_pays:
        extra_kwargs = {'RequestPayer': 'requester'}

    next_token = 'init'
    while next_token:
        kwargs = extra_kwargs.copy()
        if next_token != 'init':
            kwargs.update({'ContinuationToken': next_token})

        resp = s3_client.list_objects_v2(
            Bucket=bucket, Prefix=prefix, **kwargs)

        try:
            next_token = resp['NextContinuationToken']
        except KeyError:
            next_token = None

        for contents in resp['Contents']:
            key = contents['Key']
            yield key

将像

一样使用
x = list(get_keys('aws-naip', 'co', requester_pays=True))

答案 2 :(得分:0)

我有同样的问题,所以这里是代码:

import boto3

s3 = boto3.resource('s3')

for bucket in s3.buckets.all():
    print(bucket.name)

client = boto3.client('s3')

result= client.list_objects(Bucket='bucketname',RequestPayer='requester')
for o in result['Contents']:
    print(o['Key'])

对查询的响应是字典,在该字典中还有另一个名为contents的字典,其中键是对象的路径。您可以在以下链接中查看响应字段: List_objects documentation

注意:list_objects最多返回1000个内容,因此您必须使用next_marker属性进行迭代(如果您想要完整列表,我将更新此答案)。我想你已经想出了如何设置访问密钥和密钥。如果您需要更多详细信息,请与我们联系。