6

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
northtree
  • 8,569
  • 11
  • 61
  • 80

2 Answers2

13

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.

Mayeru
  • 1,044
  • 7
  • 12
8

You have to use get_blob() to get the blob blob = bucket.get_blob(blob_name) instead of bucket.blob(), which is a factory constructor for blob object.

Look to the difference between the two functions at get_blob() and blob().

Alessandro
  • 609
  • 1
  • 4
  • 8