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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With