Difference between blob and get_blob in Google Cloud Storage's Python library
Start your free 7-days trial now!
The similarity and difference between the methods blob(~) and get_blob(~) in Google Cloud Storage (GCS)'s Python client library are as follows:
blob(~)andget_blob(~)both return aBlobobject.the main difference between
blob(~)andget_blob(~)is that,blob(~)does not actually send any request to GCS, whileget_blob(~)fetches meta-information about the blob from GCS. There are two main implications of this:calling
blob('cat.png')will not send a request to GCS so we are not sure if this file actually exists on GCS. On the other hand, callingget_blob('cat.png')will send a request to GCS to fetch the meta-information of thecat.png. If thecat.pngdoes not exist, thenNoneis returned.meta-information (e.g.
size) is available only to blob objects fetched byget_blob(~)but not byblob(~):from google.cloud import storage# Authenticate ourselves using the primary key of the service accountpath_to_private_key = './gcs-project-354207-099ef6796af6.json'client = storage.Client.from_service_account_json(json_credentials_path=path_to_private_key)bucket = storage.Bucket(client, 'example-bucket-skytowner')blob = bucket.get_blob('cat.png')print(f'[get_blob] {blob.size} bytes')blob = bucket.blob('cat.png')print(f'[blob] {blob.size} bytes')[get_blob] 160546 bytes[blob] None bytesHere, notice how
blob.sizereturnsNonewhen we use theblob(~)constructor. Also, to learn how to generate the primary key of a service account, please consult our guide here.