Upload Image to Google Cloud Storage Python
In this tutorial we are going to larn to use Python SDK provided by Google Cloud Platform to upload, download and delete files in Google Cloud Storage (GCS).
For this tutorial, y'all must take a Google cloud account with proper credentials. To create an account on GCS, go to hither.
Assuming you have created the account and besides the first project using the dashboard, let's move forward.
Requirements
For this tutorial you need to install gcloud Python SDK in the following manner:
pip install gcloud
Once done with installing gcloud, make sure you lot take added Google Cloud Storage access cardinal every bit a JSON file to the operating system environment variables. Please refer to this site to learn how to add a GCP key as a JSON file to the local machine.
Buckets In GCS
When we create a project in GCS we accept to create a bucket to go on information on information technology. And so we create a saucepan named "datacourses-007" using the GCS dashboard as shown below.
Now that we accept a bucket created within GCS, let's motion to the Python programming role.
Uploading Files To GCS
To upload files on GCS we need to import the gcloud library and create a GCS client object in the style shown below. Also, to verify whether we are in the right directory or non, we need to print a list of buckets.
import gcloud # create gcs customer storage_client = storage.Customer() buckets = list(storage_client.list_buckets()) impress(buckets)
And so here, we can verify that we are indeed in the correct directory.
Now, allow'due south upload the file "testing.txt". We accept to laissez passer the name of the file we want to upload, the proper noun of the bucket (where the file volition reside) and the proper noun of the destination file.
destination_file_name= 'testing.txt' source_file_name = 'testing.txt' bucket_name = 'datacourses-007'
At present, we become saucepan object using storage_client.
saucepan = storage_client.bucket(bucket_name)
Let's become on and create a blob object using bucket object. Hulk object will be used to upload the file to the correct destination.
hulk = bucket.blob(destination_blob_name)
Now we call blob.upload_file() to upload file.
from gcloud import storage # create storage client storage_client = storage.Client() # requite blob credentials destination_blob_name= 'testing.txt' source_file_name = 'testing.txt' bucket_name = 'datacourses-007' # become bucket object try: bucket = storage_client.saucepan(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name) impress('file: ',source_file_name,' uploaded to saucepan: ',bucket_name,' successfully') except Exception every bit east: print(e)
Let'south run the code.
So our programme prints that our file has been uploaded successfully. Let'due south verify it on the GCS dashboard.
Y'all tin can see in the image below that the file testing.txt is available on the dashboard.
Downloading File From GCS
Follow these steps to download files from Google cloud storage:
Create a storage client simply like we did while trying to upload a file.
Then we declare source_blob_name (file on bucket that we want to download), destination_file_name (name that we assign to that file on local machine) and bucket_name.
Nosotros create a bucket and blob as nosotros did at the time of uploading a file. And then, we telephone call blob.download_to_filename() method to download the file as following:
from gcloud import storage # create storage client storage_client = storage.Client() # requite blob credentials source_blob_name= 'testing.txt' destination_file_name = 'downloaded_testing.txt' bucket_name = 'datacourses-007' # get bucket object endeavor: bucket = storage_client.bucket(bucket_name) hulk = saucepan.blob(source_blob_name) hulk.download_to_filename(destination_file_name) print('file: ',destination_file_name,' downloaded from saucepan: ',bucket_name,' successfully') except Exception as due east: print(e)
Summary
In this topic, we learned to apply Google cloud provided Python SDK chosen gcloud. We learned to use gcloud in Python to upload and download files in Google Deject Storage. We too learned well-nigh buckets and project structure while creating the first project on GCS.
References
https://cloud.google.com/docs/authentication/getting-started#deject-console
https://cloud.google.com/docs
https://cloud.google.com/
stackoverflow.com
Source: https://www.datacourses.com/how-to-upload-and-download-files-from-google-cloud-storage-python-3131/
0 Response to "Upload Image to Google Cloud Storage Python"
Post a Comment