Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the blob size of Cloud Storage object?

I could get the size from gsutil.

✗ gsutil du gs://<bucket>/test/1561402306
100          gs://<bucket>/test/1561402306

I could confirm the length and content via download_as_string. However, the size property always returns None from SDK/API.

How could I get the size of Cloud Storage object without downloaded?

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
print(len(blob.download_as_string().decode()))
print(blob.size)

Output:

100
None
like image 330
northtree Avatar asked Dec 03 '22 18:12

northtree


1 Answers

to obtain the object's metadata you should use the method "get_blob" when retrieving the blob.

I have edited your code like this:

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket(bucket_name)
blob = bucket.get_blob(blob_name) #here you use the method get_blob
print(len(blob.download_as_string().decode()))
print(blob.size)

You'll be able to access the size of the object now and you will also have access to the other metadata, for more information about it check this documentation.

like image 70
Mayeru Avatar answered Dec 29 '22 10:12

Mayeru