如何管理具有超过500个结果的GCP/Google API客户端调用

发布于 2025-02-11 19:41:35 字数 679 浏览 1 评论 0原文

在下面采样python代码。正如预期的那样,由于' MaxResults'> MaxResults 0和500,我收到不超过500个项目:

from googleapiclient import discovery

resource_object = discovery.build('compute', 'v1')
result = resource_object.firewalls().list(project="myproject-123456").execute()
print(len(result.get('items', [])))
500

但是我知道有500多个项目。

在AWS/BOTO3中,通过。 GCP有等效吗?

Sample Python code below. As expected, since 'maxResults' must be between 0 and 500, I get no more than 500 items back:

from googleapiclient import discovery

resource_object = discovery.build('compute', 'v1')
result = resource_object.firewalls().list(project="myproject-123456").execute()
print(len(result.get('items', [])))
500

But I know there are more than 500 items.

In AWS / boto3, this problem is solved via Paginators. Is there an equivalent in GCP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2025-02-18 19:41:35

找到这样做的方法是使用 pagetoken 。找不到任何python示例,所以这里是:

PROJECT_ID = "myproject-123456"

results = []
page_token = None
while True:
    result = resource_object.firewalls().list(project=PROJECT_ID, pageToken=page_token).execute()
    results.extend(result.get('items', []))
    page_token = result.get('nextPageToken')
    if not page_token:
        break

Found the way to do this is using pageToken. Couldn't find any Python examples so here goes:

PROJECT_ID = "myproject-123456"

results = []
page_token = None
while True:
    result = resource_object.firewalls().list(project=PROJECT_ID, pageToken=page_token).execute()
    results.extend(result.get('items', []))
    page_token = result.get('nextPageToken')
    if not page_token:
        break
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文