Adrian Humphrey
Adrian Humphrey

Reputation: 450

How do I list all the objects in a folder of a Google Cloud Bucket in Python?

In python the way I get my bucket is:

gs_conn = connect_gs(gs_access_key_id='accessid', gs_secret_access_key='secretaccesskey')
gs_conn.get_bucket(bucketname)

I can list all of the objects in that bucket by:

for obj in gs_conn.get_bucket(bucketname):
    print obj.name

However, I would like to list all of the objects inside of a folder inside of a bucket. For example I want to list of all the objects inside of /Animals/Dogs/. How do I do this in Python.

Upvotes: 2

Views: 842

Answers (1)

jterrace
jterrace

Reputation: 67163

You can use the get_all_keys method of the bucket. For example:

bucket = gs_conn.get_bucket(bucketname)
for obj in bucket.get_all_keys(prefix='Animals/Dogs/'):
    print obj.name

Upvotes: 1

Related Questions