我正在尝试使用以下代码从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。根据文档-它是默认的max_results for page,因为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():
将在包含路径 的页面上迭代
这意味着第一个返回的是path
的迭代器pathlist = []
for path in paths: # Should iterate ALL
pathlist.append(path.name)
第二个循环将迭代路径页,因此需要两个循环。如果你建立一个网页,你需要逐页的结果(如谷歌/必应结果等),这个是有用的
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个路径,我假设这意味着你知道你的账户中有更多的路径。如果确实是这样,应该对它进行bug调查,因为第一种语法应该返回所有,而不仅仅是第一页,请在这里打开一个问题:https://github.com/Azure/azure-sdk-for-python/issues
(我在微软Azure Python SDK团队工作)