python - 谷歌云存储 : Python API get blob information with wildcard

标签 python google-cloud-platform google-cloud-storage

我正在尝试从存储桶中获取 blob 信息,但我想在 blob 名称中使用通配符。考虑我的桶

$ gsutil ls gs://myBucket/myPath/
gs://myBucket/myPath/
gs://myBucket/myPath/ranOn=2018-12-11/
gs://myBucket/myPath/ranOn=2018-12-12/
gs://myBucket/myPath/ranOn=2018-12-13/
gs://myBucket/myPath/ranOn=2018-12-14/
gs://myBucket/myPath/ranOn=2018-12-15/
gs://myBucket/myPath/ranOn=2019-02-18/
gs://myBucket/myPath/ranOn=2019-02-19/
gs://myBucket/myPath/ranOn=2019-02-20/
gs://myBucket/myPath/ranOn=2019-02-21/

现在从命令行,我可以做到
$ gsutil ls gs://myBucket/myPath/ranOn=2018*
gs://myBucket/myPath/
gs://myBucket/myPath/ranOn=2018-12-11/
gs://myBucket/myPath/ranOn=2018-12-12/
gs://myBucket/myPath/ranOn=2018-12-13/
gs://myBucket/myPath/ranOn=2018-12-14/
gs://myBucket/myPath/ranOn=2018-12-15/

因此我可以对尺寸做同样的事情
$ gsutil du -sh gs://myBucket/myPath/ranOn=2018*
2.7 G

现在,我想用 python api 做同样的事情。这是我尝试过的
from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('myBucket')
blob = bucket.get_blob('myPath/ranOn=2018*')
print('Size: {} bytes'.format(blob.size))
Size: None bytes

为什么这不起作用?如何在带有 python api 的 blob 路径中使用通配符?

最佳答案

不幸的是get_blob仅用于获取单个文件,而不是多个文件。

您需要遍历所有与前缀匹配的文件并将它们的大小相加以获得总大小。

blobs = bucket.list_blobs(prefix="myPath/ranOn=2018")

total = sum([blob.size for blob in blobs])

关于python - 谷歌云存储 : Python API get blob information with wildcard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57082158/

相关文章:

python - Flask、mod_wsgi 和 Apache : ImportError

kubernetes - 启用Istio的GKE群集无法与Google Service Infrastructure API可靠地通信

node.js - 使用 Google Cloud API、Node.js 和 request.del() 删除 Google 云存储对象

boto - gsutil 在 GCE 中不工作

Python 相当于 unix cksum 函数

python - 如何转换为批量插入

python - 无法将带有 Poetry 的 Python 工件发布到 Google Artifact Registry,因为 "HTTP Error 413: Request Entity Too Large"

node.js - Google Cloud 功能 - 存储 - 删除图像 - "ApiError: Error during request"

python - 使用自定义重量进行训练

google-cloud-platform - 为什么需要向 Cloud SQL 服务提供外部 IP 进行授权?