Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Walking a directory tree inside a Google Cloud Platform bucket in Python

For directories on a local machine, the os.walk() method is commonly used for walking a directory tree in Python.

Google has a Python module (google.cloud.storage) for uploading to and downloading from a GCP bucket in a locally-run Python script.

I need a way to walk directory trees in a GCP bucket. I browsed through the classes in the google.cloud Python module, but could not find anything. Is there a way to perform something similar to os.walk() on directories inside a GCP bucket?

like image 831
S.G. Avatar asked Jul 06 '18 16:07

S.G.


People also ask

How do I upload a folder to Google Cloud using Python?

def upload_files(bucketName): """Upload files to GCP bucket.""" files = [f for f in listdir(localFolder) if isfile(join(localFolder, f))] for file in files: localFile = localFolder + file blob = bucket. blob(bucketFolder + file) blob. upload_from_filename(localFile) return f'Uploaded {files} to "{bucketName}" bucket.


2 Answers

No such function exists in the GCS library. However, GCS can list objects by prefix, which is usually sufficiently equivalent:

from google.cloud import storage

bucket = storage.Client().get_bucket(bucket_name)
for blob in bucket.list_blobs(prefix="dir1/"):
  print(blob.name)
like image 114
Brandon Yarbrough Avatar answered Oct 19 '22 05:10

Brandon Yarbrough


import os
from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('bucket_name')

for blob in bucket.list_blobs(prefix=''):
   # Download the file
    with open(blob.name, 'wb') as file_obj:
        client.download_blob_to_file(blob, file_obj)

   # You logic on the file
   # logic goes here

   # Remove the local file
   os.remove(blob.name)
like image 36
Alon Barad Avatar answered Oct 19 '22 03:10

Alon Barad