Reputation: 6065
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.
How can I make the library explicitly load actual data into the metadata attributes of the blob, instead of defaulting everything to None
?
Upvotes: 7
Views: 9587
Reputation: 81454
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
Upvotes: 6
Reputation: 352
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
Upvotes: 6