How to get file size of objects from google cloud python library?

Question:

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?

Asked By: AlanSTACK

||

Answers:

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

Answered By: John Hanley

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
Answered By: orby