Reading Files from Google Cloud Storage using Python


It is very common to develop Python applications on Google Cloud Platform that read various files as blobs, which are then used for some further processing. To be able to achieve this ensure you have created a service account and you have downloaded the private key credential file as a JSON.  This is used to authenticate all requests sent to GCP resources such as Cloud Storage by the Python application.

Next you will deploy your application either on Google App Engine or as a Google Cloud Function with the following Python code:

from import storage

# Initialize a storage client
storage_client = storage.Client.from_service_account_json('/<path-to-credentials>/<appl_credentials>.json')

# Define storage bucket with name
bucket = storage_client.get_bucket('<storage-bucket-name>')

# Get bucket data as blob
blob = bucket.get_blob('<blob-name>')

# Download and store blob in a file
with open('/tmp/<file-name>', 'wb') as file_obj: 


Author: daltonwhyte

A technocrat who believes in a smart future, that will be proliferated with systems that allow us to focus on the bigger picture.

Leave a Reply

Your email address will not be published. Required fields are marked *