Google cloud storage 带有前缀参数的正则表达式,用于带有前缀()的列表\u blob\u

Google cloud storage 带有前缀参数的正则表达式,用于带有前缀()的列表\u blob\u,google-cloud-storage,google-api-python-client,Google Cloud Storage,Google Api Python Client,我试图使用python api客户端从带有前缀的gcp存储中获取对象,但前缀参数有问题。我可以用gsutil和 gsutil ls-h gs://{{{bucket name}}/*/latest/ 但不是用pythonapi 我正在使用文档中的函数。 尝试将前缀参数传递为 */latest/ /*/最新版本 * 并将分隔符设为无。仍然没有得到任何结果 storage_client = storage.Client() # Note: Client.list_blobs req

我试图使用python api客户端从带有前缀的gcp存储中获取对象,但前缀参数有问题。我可以用gsutil和

gsutil ls-h gs://{{{bucket name}}/*/latest/

但不是用pythonapi

我正在使用文档中的函数。 尝试将前缀参数传递为

*/latest/
/*/最新版本
*

并将分隔符设为无。仍然没有得到任何结果

    storage_client = storage.Client()

    # Note: Client.list_blobs requires at least package version 1.17.0.
    blobs = storage_client.list_blobs(bucket_name, prefix=prefix,
                                      delimiter=delimiter)

    print('Blobs:')
    for blob in blobs:
        print(blob.name)

    if delimiter:
        print('Prefixes:')
        for prefix in blobs.prefixes:
            print(prefix)
预期产量为

gs://{{bucket-name}}/product/latest/:
gs://{{bucket-name}}/product/latest/health
gs://{{bucket-name}}/product/latest/index.html

gsutil知道正则表达式,但GCS API本身不知道。这些API只支持文字前缀

相反,您需要自己获取所有内容并使用正则表达式进行过滤,这就是gsutil在您的示例中所做的

all_blobs = storage_client.list_blobs(bucket_name)
regex = re.compile(r'.*/latest/.*')
blobs = filter(regex.match, all_blobs)
如果您将有太多的对象,因此不值得这样做,我建议您重新组织数据,使其能够在路径的开始处放置非通配符匹配,以便可以筛选服务器端