zacha2
zacha2

Reputation: 223

Accessing images from google cloud bucket (smiliar to folder)

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

Answers (3)

Sugimiyanto
Sugimiyanto

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

Pawel Czuczwara
Pawel Czuczwara

Reputation: 1520

  1. I would recommend to use google-cloud-storage Python SDK to access and manipulate bucket objects. Here you have Python Google Cloud Storage SDK examples. Here is another example for Cloud Functions for accessing object
  2. Another option is to mount your bucket as folder using gcfuse

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

Bolchojeet
Bolchojeet

Reputation: 503

You could:

  1. List the objects in a bucket using the GET Bucket API

  2. Load each one using the GET Object API

Upvotes: 1

Related Questions