Google Cloud Storage Python客户端库中list_blobs函数中的分页如何工作 [英] How does paging work in the list_blobs function in Google Cloud Storage Python Client Library

查看:119
本文介绍了Google Cloud Storage Python客户端库中list_blobs函数中的分页如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用客户端库获取Google Cloud Storage存储桶中所有Blob的列表对于Python .

根据文档,我应该使用list_blobs()功能.该函数似乎使用两个参数max_resultspage_token来实现分页.我不确定如何使用它们.

According to the documentation I should use the list_blobs() function. The function appears to use two arguments max_results and page_token to achieve paging. I am not sure how use them.

尤其是我从哪里获得page_token?

In particular, where do I get the page_token from?

我希望list_blobs()提供一个page_token供以后的调用使用,但是我找不到关于它的任何文档.

I would have expected that list_blobs() would provide a page_token for use in subsequent calls, but I cannot find any documentation on it.

此外,max_results是可选的.如果我不提供该怎么办?有默认限制吗?如果是这样,那是什么?

In addition, max_results is optional. What happens if I don't provide it? Is there a default limit? If so, what is it?

推荐答案

list_blobs()确实使用分页,但是您不使用page_token来实现分页.

list_blobs() does use paging, but you do not use page_token to achieve it.

list_blobs()的工作方式是返回一个迭代器,该迭代器对所有结果进行迭代,并在幕后进行分页.因此,只需执行此操作即可获得所有结果,并根据需要提取页面:

The way list_blobs() work is that it returns an iterator that iterates through all the results doing paging behind the scenes. So simply doing this will get you through all the results, fetching pages as needed:

for blob in bucket.list_blobs()
    print blob.name

文档有误/误导:

截至2017年4月26日,文档内容如下:

The Documentation is Wrong/Misleading:

As of 04/26/2017 this is what the docs says:

page_token(str)–(可选)不透明标记,用于以下内容的页面" 斑点.如果未通过,将返回Blob的第一页.

page_token (str) – (Optional) Opaque marker for the next "page" of blobs. If not passed, will return the first page of blobs.

这意味着结果将是结果的单页,而page_token确定哪一页.这是不正确的.结果迭代器遍历多个页面. page_token实际代表的是迭代器应在开始所在的页面上.没有提供page_token,它将从第一页开始.

This implies that the result will be a single page of results with page_token determining which page. This is not correct. The result iterator iterates through multiple pages. What page_token actually represents is which page the iterator should START at. It no page_token is provided it will start at the first page.

max_results限制了迭代器返回的结果总数.

max_results limits the total number of results returned by the iterator.

如果需要,迭代器会公开页面:

The iterator does expose pages if you need it:

for page in bucket.list_blobs().pages:
    for blob in page:
        print blob.name

这篇关于Google Cloud Storage Python客户端库中list_blobs函数中的分页如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆