7

Problem

Hello everyone. I am attempting to obtain the file size of an object using the google-cloud python library. This is my current code.

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket("example-bucket-name")
object = bucket.blob("example-object-name.jpg")

print(object.exists())
>>> True

print(object.chunk_size)
>>> None

It appears to me that the google-cloud library is choosing not to load data into the attributes such as chunk_size, content_type, etc.

Question

How can I make the library explicitly load actual data into the metadata attributes of the blob, instead of defaulting everything to None?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99

2 Answers2

6

Call get_blob instead of blob.

Review the source code for the function blob_metadata at this link. It shows how to get a variety of metadata attributes of a blob, including its size.

If the above link dies, try looking around in this directory: Storage Client Samples

Stephen
  • 8,508
  • 12
  • 56
  • 96
John Hanley
  • 74,467
  • 6
  • 95
  • 159
5

Call size on Blob.

from google.cloud import storage

# create client
client: storage.client.Client = storage.Client('projectname')

# get bucket
bucket: storage.bucket.Bucket = client.get_bucket('bucketname')

size_in_bytes = bucket.get_blob('filename').size
orby
  • 267
  • 4
  • 6
  • 2
    @SuperEye - The code works. I just verified that on my system. Change the projectname, bucketname, and filename. No issues. What problem did you experience? – John Hanley Apr 25 '21 at 22:20
  • Yes, this is correct, bucket.get_blob('filename').size works fine. – london_utku Apr 26 '21 at 07:13