简体   繁体   English

清洁 Boto3 分页

[英]Clean Boto3 Pagination

I am trying to find a very nice python idiom to use aws boto3 paginators in the most "pythonic" way.我试图找到一个非常好的 python 成语以最“pythonic”的方式使用 aws boto3 paginators。 Below is the best I have been able to come up with and I'm still not happy with it.以下是我能想到的最好的,但我仍然不满意。 Any ideas on how to make pagination simpler, possibly not using while True: ?关于如何使分页更简单的任何想法,可能不使用while True:

import boto3

client = boto3.client('acm', region_name='ap-southeast-2')

paginator = client.get_paginator('list_certificates')
response_iterator = paginator.paginate()

while True:
    for certificates in response_iterator:
        for certificate in certificates['CertificateSummaryList']:
            print(certificate)

    if response_iterator.resume_token:
        response_iterator = paginator.paginate(
            PaginationConfig={
                'StartingToken': response_iterator.resume_token
            })
    else:
        break

Woudn't the following form work?:下面的表格行不通?:

client = boto3.client('acm', region_name='ap-southeast-2')

paginator = client.get_paginator('list_certificates')

for page in paginator.paginate():
    print(page)

It's not really documented but you can do something like this with paginators它没有真正记录,但你可以用分页器做这样的事情

client = boto3.client('acm')
results = (
    client.get_paginator('list_certificates')
    .paginate()
    .build_full_result()
)
print(results)

edit编辑

a similar approach for ec2 which has one of the more annoying response structures: ec2的类似方法具有更烦人的响应结构之一:


import jmespath

client = boto3.client('ec2')
results = (
   client.get_paginator('describe_instances')
   .paginate()
   .build_full_result()
)

# jmespath could be replaced with some nested for loops if you prefer, but I find this "cleaner" and easier to read.

instances = jmespath.search('Reservations[].Instances[]')

for i in instances:
  print(i['InstanceId'])

You can generate an example output response which is super handy for most, if not all, the services which has probably saved me days in trying to figure.您可以生成一个示例 output 响应,这对于大多数(如果不是全部)服务来说非常方便,这可能让我在尝试计算时节省了很多时间

Unfortunately, I think there's a bug in the aws-cli skeleton output for acm list-certificates不幸的是,我认为acm list-certificates的 aws-cli 框架 output 中存在错误

but the equivalent for ec2.describe_instances just for fun.ec2.describe_instances的等价物只是为了好玩。

$ aws ec2 describe-instances --generate-cli-skeleton output
{
    "Reservations": [
        {
            "Groups": [
                {
                    "GroupName": "GroupName",
                    "GroupId": "GroupId"
                }
            ],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ImageId",
                    "InstanceId": "InstanceId",
                    "InstanceType": "InstanceType",
                    "KernelId": "KernelId",
                    "KeyName": "KeyName",
                    "LaunchTime": "1970-01-01T00:00:00",
                    "Monitoring": {
                        "State": "State"
                    },
  ...

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

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