获取 blob 存储 GCP 的大小

get size of blob storage GCP

我正在使用 python 和 airflow(composer)

我的 GCP 存储桶中有很多 csv 文件,我需要获取特定文件的大小。

文件名来自之前。 我正在使用 list_blobs 函数,但我必须使用 for 来搜索文件,没有获取特定 blob 信息的函数吗?

 from google.cloud.storage import Blob
 from google.cloud import storage

 client = storage.Client()
 bucket = client.bucket('gs://bucket_name')
 desired_file = kwargs['csv_name']

  for blob in bucket.list_blobs():
    if desired_file== blob.name and blob.size > 0:
        print("Name: "+ blob.name +" Size blob obj: "+str(blob.size) + "bytes")

您可以利用函数 bucket.get_blob('filename') 获得所需的 blob 而不是循环 bucket.list_blobs()

from google.cloud.storage import Blob
from google.cloud import storage

client = storage.Client()
bucket = client.bucket('gs://bucket_name')
desired_file = kwargs['csv_name']

# new code
blob = bucket.get_blob(desired_file)

print("Name: "+ blob.name +" Size blob obj: "+str(blob.size) + "bytes")