Reputation: 223
I want to load all images I have stored in a bucket with a python script. To load the locally stored images I'm using the following code.
images_dir = os.path.join(current_dir, 'image_data')
images = []
for each in os.listdir(images_dir):
images.append(os.path.join(images_dir,each))
Is there a way to access buckets in a folder-like style?
Upvotes: 1
Views: 2153
Reputation: 329
To load all images, you need to list all the available files from google cloud storage bucket using list_blobs().
Then you can download the objects in a loop. Here is an example:
from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket("YOUR_BUCKET_NAME")
blobs = bucket.list_blobs(prefix="YOUR_FOLDER_CONTAINS_IMAGES")
images = []
for idx, bl in enumerate(blobs):
if idx == 0:
continue
data = bl.download_as_string()
images.append(data)
Don't forget to set the GOOGLE_APPLICATION_CREDENTIALS environment variable for authentication. Hope it helps.
Upvotes: 3
Reputation: 1520
Using SDK please be aware that object gs://your-bucket/abc/def.txt is just an object that happens to have "/" characters in its name. There is no "abc" directory; just a single object with the given name. Here is documentation that explains how directories work.
Upvotes: 0
Reputation: 503
You could:
List the objects in a bucket using the GET Bucket API
Load each one using the GET Object API
Upvotes: 1