在Boto3中,如何为带有附加关键字参数的list_objects创建分页器? [英] In Boto3, how to create a Paginator for list_objects with additional keyword arguments?
问题描述
I'm using a Paginator
to iterate over the contents of an S3 bucket (following http://boto3.readthedocs.io/en/latest/guide/paginators.html#creating-paginators):
client = boto3.client('s3')
paginator = client.get_paginator('list_objects')
page_iterator = paginator.paginate(Bucket=<my_bucket>)
for page in page_iterator:
for object in page['Contents']:
key = object['Key']
在此示例中,方法名称'list_objects'
作为字符串传递.但是,实际上我想使用带有关键字参数Prefix="files/"
的'partial'函数list_objects
,因为我感兴趣的对象位于files/
'目录'中(请参阅.
In this example, the method name 'list_objects'
is passed as a string. However, I would actually like to use a 'partial' function list_objects
with the keyword argument Prefix="files/"
, since the objects I'm interested in are in the files/
'directory' (cf. http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.list_objects).
这可能吗? (从文档中我不清楚如何执行此操作;我可以在双for
循环中执行if key.startswith("files/")
,但这似乎不是一个很好的解决方案).
Is this possible? (It's not clear to me from the documentation how to do this; I could do an if key.startswith("files/")
in the double for
loop, but this does not seem like an elegant solution).
推荐答案
在同一文档中您是否错过了? 过滤结果
Did you miss this in the same document? Filtering results
S3.Paginator.list_objects.paginate()接受前缀参数,用于在发送给客户端之前通过服务器端添加前缀来过滤分页结果:
S3.Paginator.list_objects.paginate() accepts a Prefix parameter used to filter the paginated results by prefix server-side before sending them to the client:
client = boto3.client('s3', region_name='us-west-2')
paginator = client.get_paginator('list_objects')
operation_parameters = {'Bucket': 'my-bucket',
'Prefix': 'foo/baz'}
page_iterator = paginator.paginate(**operation_parameters)
for page in page_iterator:
print(page['Contents'])
这篇关于在Boto3中,如何为带有附加关键字参数的list_objects创建分页器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!