Upload Files to Google Cloud Storage with Python
This tutorial will lead you to your first file upload to the Google Cloud Platform (GCP).
Join the DZone community and get the full member experience.
Join For FreeGoogle Cloud is a suite of cloud-based services just like AWS from Amazon and Azure from Microsoft. AWS dominates the market with Azure but Google's not far behind. Google Cloud Platform or GCP is the third largest cloud computing platform in the world, with a share of 9% closely followed by Alibaba Cloud.
Amazon undoubtedly leads the market with a share of 33% but GCP is showing tremendous spike with the growth rate of whooping 83% in 2019. GCP leads AWS on the cost front, though. Google has a lesser number of services to offer but maintains its position as one of the most cost-effective cloud platform.
Using GCP with Python
Python has been a go-to language for every modern age technology including Data Science, Machine Learning, Big Data, and Cloud. Just like other Cloud giants, GCP too supports Python.
This blog will focus on the storage service offered by Google called Google Cloud Storage or GCS. GCS can be used in python by installing google-cloud-storage API client library.
Google Cloud Storage allows you to store data on Google infrastructure with very high reliability, performance, and availability and can be used to distribute large data objects to users via direct download.
The Credentials
Accessing GCS through Python API requires credentials to be stored locally on your system in a JSON file format, which can be downloaded from the IAM and Admin service.
It requires the creation of a new service account.
The Code
xxxxxxxxxx
from google.cloud import storage
# Setting credentials using the downloaded JSON file
client = storage.Client.from_service_account_json(json_credentials_path='credentials-python-storage.json')
# Creating bucket object
bucket = client.get_bucket('py-python')
# Name of the object to be stored in the bucket
object_name_in_gcs_bucket = bucket.blob('my_first_gcs_upload.png')
# Name of the object in local file system
object_name_in_gcs_bucket.upload_from_filename('gcs.png')
You would now be able to see your first file uploaded on the GCS bucket using a simple python code snippet.
I am glad you made this far in the blog. Hope you liked the tutorial.
If you need any assistance in making a new Google cloud account, do let me know in the comments section and I will be happy to help.
Opinions expressed by DZone contributors are their own.
Comments