How does paging work in the list_blobs function in Google Cloud Storage Python Client Library

前端 未结 3 1944
深忆病人
深忆病人 2021-02-20 02:42

I want to get a list of all the blobs in a Google Cloud Storage bucket using the Client Library for Python.

According to the documentation I should use the list_bl

3条回答
  •  深忆病人
    2021-02-20 03:08

    list_blobs() does use paging, but you do not use page_token to achieve it.

    How It Works:

    The way list_blobs() work is that it returns an iterator that iterates through all the results doing paging behind the scenes. So simply doing this will get you through all the results, fetching pages as needed:

    for blob in bucket.list_blobs()
        print blob.name
    

    The Documentation is Wrong/Misleading:

    As of 04/26/2017 this is what the docs says:

    page_token (str) – (Optional) Opaque marker for the next “page” of blobs. If not passed, will return the first page of blobs.

    This implies that the result will be a single page of results with page_token determining which page. This is not correct. The result iterator iterates through multiple pages. What page_token actually represents is which page the iterator should START at. It no page_token is provided it will start at the first page.

    Helpful To Know:

    max_results limits the total number of results returned by the iterator.

    The iterator does expose pages if you need it:

    for page in bucket.list_blobs().pages:
        for blob in page:
            print blob.name
    

提交回复
热议问题