Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Blob prefixes not available until you iterate the files #863

Closed

Description

@jaymeedwards

Environment details

  • OS type and version: Windows 10
  • Python version: 3.9.13
  • pip version: 22.0.4
  • google-cloud-storage version: 2.5.0

Steps to reproduce

  1. Download the sample code for listing files with a prefix.
  2. Comment out the lines 56-58 that print the names of each blob.
  3. Create a google cloud storage bucket with a subfolder, and a file within that subfolder.
  4. Run the sample where it specifies the subfolder as the prefix with slash at the end (as shown) and the delimiter of '/'.
  5. Nothing is displayed (the prefixes are empty).
  6. Un-comment the lines that print the names of each blob.
  7. Run it again.
  8. The prefixes are displayed.

It appears you cannot list the prefixes unless you iterate the files first. This seems strange.

If it's intended, maybe this library should pre-iterate the list so this doesn't happen? Or at least document that this is expected?

Activity

  1. added
    samplesIssues that are directly related to samples.
    type: questionRequest for information or clarification. Not an issue.
    on Sep 12, 2022
  2. self-assigned this
    on Sep 12, 2022
  3. cojenco commented on Sep 13, 2022

    @cojenco
    Contributor

    Hi eavonius, thank you for the question. You are right about the iterator has to be called first in order to access the list of prefixes. This is intended, as list_blobs operation returns an iterator, and the rpc is done with a response back only when you iterate through the iterator

    Another way that works too:

    blobs = storage_client.list_blobs(bucket_name, prefix=prefix, delimiter=delimiter)
    
    list(blobs)
    print(blobs.prefixes)
    

    I agree this could stand to be clarified in the documentation. I'll update the official documentation and sample notes.

  4. jaymeedwards commented on Sep 14, 2022

    @jaymeedwards
    Author

    @cojenco thanks, makes sense to me. Yep a little note in the documentation would go a long way. Was very confusing at first. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/python-storage API.samplesIssues that are directly related to samples.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions