是否可以像在终端中使用`ls`一样查询Google Cloud Storage? [英] Is it possible to query Google Cloud Storage similar to using `ls` command in terminal?

查看:108
本文介绍了是否可以像在终端中使用`ls`一样查询Google Cloud Storage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python库查询Google Cloud Storage,并且正在使用命名层次结构来组织Storage中的信息.例如:

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...

我的问题是:是否可以使用list_blobs或其他方法进行查询,以检索仅包含模拟"目录中的版本而不是模拟下方所有blob的列表?

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?

作为参考,这将以分页方式返回所有blob:

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

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

推荐答案

我一直使用list_blobs方法的prefixdelimiter参数,并且此代码有效:

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/', '/')

输出:

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

请注意,这只会显示simulations/目录中的目录的名称,这些文件将被省略.

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

这篇关于是否可以像在终端中使用`ls`一样查询Google Cloud Storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆