pythongoogle-cloud-storagegoogle-api-python-client

Get size of file using blob in Google Cloud


I use this code to get object of file in google cloud.

client = storage.Client.from_service_account_json("./credential.json")
bucket = client.get_bucket(bucket)
object = bucket.blob(fileurlName)

and I have url of file in variable fileurlName, but the results size is None and chunk size is not the same as file size so how can I change it to give me the exact size of file in Google Cloud?


Solution

  • It's quite tricky.

    In fact, when you use the method of the type , you simply ask the client library to build a correct path to access to the object. But you don't access it, you only build a local reference to it. And thus all the value (or almost) are none.

    If you want to GET the object, use the get_ method.

    In your case replace the bucket.blob(fileurlName) by bucket.get_blob(fileurlName). This time, the object metadata are really fetched (the API call is performed) and the size field is populated.