{{announcement.body}}
{{announcement.title}}

Upload Files to Google Cloud Storage with Python

DZone 's Guide to

Upload Files to Google Cloud Storage with Python

This tutorial will lead you to your first file upload to the Google Cloud Platform (GCP).

· Cloud Zone ·
Free Resource

Google 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.

New service account

The Code

Python
 




xxxxxxxxxx
1
14


 
1
 
          
2
from google.cloud import storage
3
 
          
4
# Setting credentials using the downloaded JSON file
5
client = storage.Client.from_service_account_json(json_credentials_path='credentials-python-storage.json')
6
 
          
7
# Creating bucket object
8
bucket = client.get_bucket('py-python')
9
 
          
10
# Name of the object to be stored in the bucket
11
object_name_in_gcs_bucket = bucket.blob('my_first_gcs_upload.png')
12
 
          
13
# Name of the object in local file system
14
object_name_in_gcs_bucket.upload_from_filename('gcs.png')



Bucket details
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.

Topics:
aws s3, buckets, gcp, gcs, google, google cloud platform, python, s3 bucket, s3 file upload, upload files

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}