2

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).

3

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

3 Comments

@GauravMantri-AIS I don't see how pointing to a results_per_page arg shows this is incorrect
OP is looking for an equivalent of maxresults (available in REST API) in Python SDK. results_per_page does exactly that.
@GauravMantri-AIS: What is incorrect? I am looking for a way to limit results from list_blobs()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.