google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

List subdirectories

Open samskillman opened this issue 11 years ago • 29 comments

In order to navigate (potentially broad) directory hierarchies in buckets, it would be useful to be able to list the sub-directories in a bucket at a given prefix. Happy to attempt to implement such a method if someone points me in the correct direction. Seems like perhaps a new Iterator?

samskillman avatar Jun 09 '15 05:06 samskillman

As iterator.get_next_page_response is hidden nowadays, you can use the following code:

def get_prefixes(bucket):
    iterator = bucket.list_blobs(delimiter="/")
    response = iterator._get_next_page_response()
    return response['prefixes']

JorritPosthuma avatar Jul 06 '17 12:07 JorritPosthuma

@JorritPosthuma Thanks! To make sure I get all the prefixes, even for large buckets, and using the "public" API, I wrote the following:

def list_gcs_directories(bucket, prefix):
    # from https://github.com/GoogleCloudPlatform/google-cloud-python/issues/920
    iterator = bucket.list_blobs(prefix=prefix, delimiter='/')
    prefixes = set()
    for page in iterator.pages:
        print page, page.prefixes
        prefixes.update(page.prefixes)
    return prefixes

evanj avatar Aug 30 '17 21:08 evanj

Feature Requests will now be tracked in our Project Feature Request. Please feel free to continue the discussions here.

chemelnucfin avatar Jan 22 '18 18:01 chemelnucfin

@evanj I like your code, but have a strong feeling that it x times slower than gsutil ls specially for buckets with a lot of files deep in the three. Are there some options to optimize the Python code or is better to do system call of gsutil ls for speed up the listing of sub-folders?

VelizarVESSELINOV avatar Mar 20 '19 12:03 VelizarVESSELINOV

It should be making a very similar API calls. I suspect if you know the bucket is large, you could attempt to list files using multiple API calls in parallel, by splitting the file name space. I've never had to do such a thing. Good luck!

evanj avatar Mar 20 '19 13:03 evanj

ya, i 'm trying to write a web interface so that some images can be hand checked. I have ~3500 groups (subfolders) and each has 10,000 images (so 35,000,000 blobs). I need to select a random sample from each group as it would be next to impossible to hand check them all. I can't loop over all the the blobs just to get the prefixes every page load. Groups a added and removed constantly hence I'd like the bucket to be the single source of truth. I guess GCS is not great for BIG DATA :(

ghost avatar Dec 05 '19 22:12 ghost

@whillas I'm doing something similar; but I use Firestore as the "directory structure" if you will, and only keep the actual image assets in GCS. The firestore queries can be made to be very fast, then you have the GCS URLs right there.

garyo avatar Dec 12 '19 19:12 garyo

The code from @evanj works wonders! Here is a version for python3 and using the client directly since bucket.list_blobs is deprecated:

def list_gcs_directories(client, bucket, prefix):
    # from https://github.com/GoogleCloudPlatform/google-cloud-python/issues/920
    iterator = client.list_blobs(bucket, prefix=prefix, delimiter='/')
    prefixes = set()
    for page in iterator.pages:
        print (page, page.prefixes)
        prefixes.update(page.prefixes)
    return prefixes

haehn avatar Jul 04 '20 23:07 haehn

I agree with comments above. Not being able to list top level directories and files for a given prefix is really annoying. This is not big data at all :). We are currently stuck because we cannot properly list files from Google Storage...... And the best part here :

  • If you use gsutil command, this is possible
  • GCP storage interface console is able to do it.

As I guess both gsutil and Console are using an API, why don't you open functionality ????

Plus, if you propose to index files in some database, I think this is a very bad idea and design. You have to maintain DB and filesystem synchronized... Which always leads to errors with time and high volumes.....

lilacdev avatar Dec 15 '20 13:12 lilacdev