Azure ItemPaged 迭代器应该如何工作?

问题描述

我正在尝试使用以下代码从 ADLS Gen2 存储中检索所有路径:

file_system_client = service_client.get_file_system_client(file_system="my-file-system")
paths = file_system_client.get_paths()
pathlist = []
for path in paths:
    pathlist.append(path.name)

pathlist 的长度是 5000。根据 documentation - 它是页面认 max_results,因为 get_pages()输出ItemPaged[PathProperties]。 >

现在我无法理解如何处理这种输出类型以从我的文件系统中获取所有路径...

我也尝试使用 by_pages() 方法迭代页面,但直到只有一页,然后分页结束:

page_iterator = paths.by_pages()
page_iterator.next()
page iterator.current_page

[list-of-PathProperties] - 5000 items

page_iterator.next()

stopiteration: End of paging

而且我肯定知道有更多的路径可以从容器中检索。

你能帮我正确处理这个程序吗?

谢谢!

解决方法

有两种迭代方式:

  • for path in file_system_client.get_paths(): 将迭代所有路径,而不是页面的概念
  • for page in file_system_client.get_paths().by_pages(): 将迭代包含路径的页面

这意味着第一个返回路径的迭代器

pathlist = []
for path in paths:  # Should iterate ALL
    pathlist.append(path.name)

虽然第二个将迭代路径页面,因此您需要两个循环。例如,如果您构建一个网页,并且您需要逐页结果(例如 Google/Bing 结果等),则此方法很有用

pathlist = []
for page in file_system_client.get_paths().by_pages():
    for path in page:
        pathlist.append(path.name)

ItemPaged 是一个迭代器,这意味着你可以使用所有需要迭代器作为输入的东西来使用它。你根本不需要for

pathlist = list(file_system_client.get_paths())  # list() consumes the iterator to a list

这是针对这些类的一般行为。

现在我从您的帖子中了解到,您希望获得 5000 多个路径,我认为这意味着您知道您的帐户中拥有更多路径。如果确实如此,则值得进行错误调查,因为第一个语法应该返回所有内容,而不仅仅是第一页,请在此处打开问题:https://github.com/Azure/azure-sdk-for-python/issues

(我在微软的 Azure Python SDK 团队工作)