简体   繁体   中英

Azure SDK for Python: How to limit results in list_blobs()?

How can the number of blobs returned from ContainerClient.list_blobs() method can be limited?

The Azure Blob service RESP API docs mentions a maxresults parameter, but it seems it is not honored by list_blobs(maxresults=123) .

A combination of itertools.islice and the results_per_page parameter (which translates to the REST maxresults parameter) will do the trick:

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)

Please use by_page() on the ItemPaged class

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

There's no way to do this currently with the SDK. The maxresults parameter really means "max results per page"; if you have more blobs than this, list_blobs will make multiple calls to the REST API until the listing is complete.

You could call the API directly and ignore pages after the first, but that would require you to handle the details of authentication, parsing the response, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM