簡體   English   中英

Google Cloud Storage:Python API 使用通配符獲取 blob 信息

[英]Google Cloud Storage : Python API get blob information with wildcard

我正在嘗試從存儲桶中獲取 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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM