我如何在 Boto3 中使用 build_full_result()

How do I use build_full_result() in Boto3

我在谷歌上搜索以了解 boto3 分页器的工作原理,并找到了一个可能不需要使用 NextToken 和 While 循环编写任何逻辑的解决方案。

不过,我不太确定我在使用它时会得到什么:

client = boto3.client('ec2', region_name='eu-west-1')
results = (
   client.get_paginator('describe_instances')
   .paginate()
   .build_full_result()
)

print(results)

我得到了一个巨大的 JSON 输出,我不确定我是否得到了我想要的,这基本上是我所有 EC2 实例的输出。

我也不确定如何遍历它,我不断得到 TypeError: string indices must be integers 这在我使用类似以下内容时没有发生过:

for instance in response_iterator:
    instance = instance['Reservations'][0]
    instance_id = instance['Instances'][0]['InstanceId']
    print(instance_id)

我很想了解如何使用 build_full_result() 方法。

我看到一个 post 说它还没有记录,最近到现在(写这篇文章时 post)。

有趣的发现..latest version of boto3 documentation 中的任何地方都没有提到这一点,但它似乎确实 return 所有可用的结果。

下面是 Lambda 的示例,展示了如何通过响应执行简单循环。您可以更新最后两行来处理 response syntax from EC2 describe instances.

import boto3

client = boto3.client('lambda')

results = (
   client.get_paginator('list_functions')
   .paginate()
   .build_full_result()
)

for result in results['Functions']:
    print(result['FunctionName'])