Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blob prefixes only exist after iterating over blobs #192

Closed
joostvham opened this issue Jun 24, 2020 · 2 comments
Closed

blob prefixes only exist after iterating over blobs #192

joostvham opened this issue Jun 24, 2020 · 2 comments
Assignees
Labels
api: storage Issues related to the googleapis/python-storage API. type: question Request for information or clarification. Not an issue.

Comments

@joostvham
Copy link

Environment details

  • Ubuntu 18.04
  • python 3.7.4
  • pip 19.0.3
  • google-cloud-storage version 1.29.0

Steps to reproduce

I have a bucket, with at the 'root' of the bucket 3 directories. I want to display these three directories. However, they only exist when iterating over the blobs first, without a clear reason.

Code example

from google.cloud import storage

bucket_name = 'bucket-name'
client = storage.Client()
bucket = client.get_bucket(bucket_name)
blobs = bucket.list_blobs(delimiter='/')

print('printing prefixes 1 ...')
print(blobs.prefixes)

[b for b in blobs]

print('printing prefixes 2 ...')
print(blobs.prefixes)

Output

python bucket.py 
printing prefixes 1 ...
set()
printing prefixes 2 ...
{'test-data/', 'unparsed-data/', 'raw-data/'}

@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/python-storage API. label Jun 24, 2020
@frankyn frankyn added the type: question Request for information or clarification. Not an issue. label Jun 24, 2020
@IlyaFaer
Copy link

IlyaFaer commented Jun 25, 2020

@joostvham, in fact, list_blobs() returns an iterator, which from the very start have prefixes explicitly set to set(). So, it's intended to work like this. One can call this iterator lazy, as it's requesting data while iterating. In other words, when you got your blobs returned from bucket.list_blobs(delimiter='/'), you don't yet have the data. Only while iterating through blobs, you'll actually retrieve it (including prefixes).

@joostvham
Copy link
Author

Thanks for your response. My bad, I should have read the documentation better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/python-storage API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants