【问题标题】:Azure SDK for Python: How to limit results in list_blobs()?适用于 Python 的 Azure SDK:如何限制 list_blob() 中的结果?
【发布时间】:2020-09-28 10:57:33
【问题描述】:

如何限制ContainerClient.list_blobs()方法返回的blob数量?

Azure Blob service RESP API docs 提到了一个maxresults 参数,但似乎list_blobs(maxresults=123) 不承认它。

【问题讨论】:

标签: python azure azure-blob-storage azure-sdk-python


【解决方案1】:

itertools.isliceresults_per_page 参数(转换为 REST maxresults 参数)的组合可以解决问题:

import itertools

service: BlobServiceClient = BlobServiceClient.from_connection_string(cstr)
cc = service.get_container_client("foo")

n = 42
for b in itertools.islice(cc.list_blobs(results_per_page=n), n):
    print(b.name)

【讨论】:

    【解决方案2】:

    请在ItemPaged class上使用 by_page()

    pages = ContainerClient.list_blobs(maxresults=123).by_page()
    first_page = next(pages)
    items_in_page = list(a_page) #this will give you 123 results on the first page
    second_page = next(pages) # it will throw exception if there's no second page
    items_in_page = list(a_page) #this will give you 123 results on the second page
    

    【讨论】:

      【解决方案3】:

      目前无法使用 SDK 执行此操作。 maxresults 参数真正的意思是“每页最大结果”;如果您有比这更多的 blob,list_blobs 将多次调用 REST API,直到列表完成。

      您可以直接调用 API 并忽略第一个页面之后的页面,但这需要您处理身份验证、解析响应等细节。

      【讨论】:

      • @GauravMantri-AIS 我看不出如何指向 results_per_page arg 表明这是不正确的
      • OP 正在 Python SDK 中寻找 maxresults 的等效项(在 REST API 中可用)。 results_per_page 正是这样做的。
      • @GauravMantri-AIS:什么不正确?我正在寻找一种方法来限制来自 list_blobs() 的结果
      猜你喜欢
      • 2020-02-13
      • 2022-06-19
      • 2012-03-24
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2021-09-19
      相关资源
      最近更新 更多