Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I loop through all the file names in a subdirectory on Google Cloud Storage with python?

Say I have some bucket/subdirectory on Google Cloud Storage and this bucket's address is:

gs://test-monkeys-example/training_data/cats

In this cats subdirectory I have a bunch of images of cats, all of which are jpgs. How would I in python loop through the cats subdirectory and print out all the names of the files in it?

Something like:

for x in directory('gs://test-monkeys-example/training_data/cats'):
    print(x)

Obviously directory('gs://test-monkeys-example/training_data/cats') is not how to do this and is just psuedocode- how would i do this?!

like image 568
sometimesiwritecode Avatar asked May 27 '17 01:05

sometimesiwritecode


People also ask

Which command line tool works with Google cloud storage buckets?

gsutil is a Python application that lets you access Cloud Storage from the command line. You can use gsutil to do a wide range of bucket and object management tasks, including: Creating and deleting buckets.

Which command is used to download the multiple files in GCP?

I would suggest downloading the files with gsutil . However if you have a large number of files to transfer you might want to use the gsutil -m option, to perform a parallel (multi-threaded/multi-processing) copy: gsutil -m cp -R gs://your-bucket .


2 Answers

Google Cloud Storage supports listing only objects that begin with a certain prefix. You can access it from the client library like so:

from google.cloud import storage

client = storage.Client()
bucket = client.bucket('mybucket')
for blob in bucket.list_blobs(prefix='training_data/cats'):
  print blob.name
like image 77
Brandon Yarbrough Avatar answered Nov 09 '22 02:11

Brandon Yarbrough


Use the storage module:

import google.datalab.storage as storage
cats = [o.key for o in storage.Bucket('test-monkeys-example').objects()
  if o.key.startswith('training_data/cats')]

This gives you a list of such cats.

Alternatively, you could use the Objects class:

cats = [o.key for o in storage.Objects('test-monkeys-example', '', '')
  if o.key.startswith('training_data/cats')]

If you don't need the list put in a variable, you can use the %gcs magic, it's easier:

%gcs list -o gs://test-monkeys-example/training_data/cats/*

This prints an HTML table of the keys. Note this is a full GCS path, starting with gs://.

like image 45
yelsayed Avatar answered Nov 09 '22 04:11

yelsayed