Is it possible to query Google Cloud Storage similar to using `ls` command in terminal?

让人想犯罪 __ 提交于 2019-12-11 05:47:34

问题


I am using the python library for querying Google Cloud Storage, and I am organizing information in Storage using a naming hierarchy. For example:

my_bucket/simulations/version_1/data...
my_bucket/simulations/version_2/data...
my_bucket/simulations/version_3/data...
my_bucket/other_data/more_data...

My question is: is it possible to query using list_blobs or some other method to retrieve a list that contains just the versions from the "simulations" directory, and not all of the blobs below simulations?

For reference, this returns all blobs in a paginated fashion:

cursor = bucket.list_blobs(prefix='simulations')

回答1:


I've played around with the prefix and delimiter parameters of list_blobs method and this code worked:

from google.cloud import storage

def ls(bucket_name, prefix, delimiter):

    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    cursor = bucket.list_blobs(prefix=prefix, delimiter=delimiter)
    for blob in cursor:
        pass

    for prefix in cursor.prefixes:
        print prefix

ls(your_bucket_name, 'simulations/', '/')

output:

simulations/version-1/
simulations/version-2/
simulations/version-3/

Note that this will only display the names of the directories inside the simulations/ directory, the files will be omitted.



来源:https://stackoverflow.com/questions/49198056/is-it-possible-to-query-google-cloud-storage-similar-to-using-ls-command-in-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!