简体   繁体   English

Python 中的分页器

[英]Paginator in Python

The code below downloads files from an S3 bucket to a local directory.下面的代码将文件从 S3 存储桶下载到本地目录。

import boto3

s3_client = boto3.client('s3')

response = s3_client.list_objects_v2(Bucket='MY-BUCKET', Prefix='foo/')
objects = sorted(response['Contents'], key=lambda obj: obj['LastModified'])

## Latest object
latest_object = objects[-1]['Key']
filename = latest_object[latest_object.rfind('/')+1:] # Remove path

# Download it to current directory
s3_client.download_file('MY-BUCKET', latest_object, filename)

The list_objects_v2 command only returns a maximum of 1000 objects. list_objects_v2命令最多只返回 1000 个对象。 I'm aware paginator could be a solution for this, since the bucket in use has more objects.我知道分页器可以解决这个问题,因为使用的桶有更多的对象。 How can this be implemented in the above?这在上面如何实现?

There is a built-in class that you can use class S3.Paginator.ListObjectsV2有一个内置的 class 你可以使用class S3.Paginator.ListObjectsV2

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Paginator.ListObjectsV2 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Paginator.ListObjectsV2

Here is how you can add paginator into your current code.以下是如何将分页器添加到当前代码中。

import boto3

s3_client = boto3.client('s3')
# Add paginator
paginator = s3_client.get_paginator('list_objects_v2')
# Use pagination
response = paginator.paginate(Bucket='MY-BUCKET', Prefix='foo/')

data = []
for r in response:
    data += [c for c in r['Contents']]

print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM