How To Integrate with Google Cloud Platform using Python

Resources
- Cloud Client Libraries - https://cloud.google.com/apis/docs/cloud-client-libraries
- Google Cloud Python | Github - https://github.com/googleapis/google-cloud-python
- Google Cloud Storage | Snippets | Github - https://github.com/googleapis/python-storage/tree/main/samples/snippets
- How to setup service account - https://cloud.google.com/iam/docs/creating-managing-service-accounts
Steps
Create a directory with service_account.json, requirements.txt, main.py, hello_gcs.txt, Dockerfile in Cloud Shell
Setup service_account.json using https://cloud.google.com/iam/docs/creating-managing-service-accounts
{
"type": "service_account",
"project_id": "abc",
"private_key_id": "abc",
"private_key":"abc",
"client_email": "abc",
"client_id": "abc",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "abc"
}
requirements.txt based on your requirements
google-cloud-storage==2.2.1
main.py, refer here for more details https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
from google.cloud import storage
"""Uploads a file to the bucket."""
# The ID of your GCS bucket
bucket_name = "chenmingyong"
# The path to your file to upload
source_file_name = "hello_gcs.txt"
# The ID of your GCS object
destination_blob_name = "hello_gcs.txt"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"Successfully uploaded {source_file_name} into gs://{bucket_name}/{destination_blob_name}")
hello_gcs.txt -> just type anything you like 🤣
Today is a sunny day!
Dockerfile, based on the guide here https://hub.docker.com/_/python
FROM python:3.7
# Install nano editor just in case we need to write some file
RUN apt-get update
RUN apt-get -y install nano
# Install the python dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy all the files in the existing folder into /usr/src/app
WORKDIR /usr/src/app
COPY . .
# Export google application credentials to have the necessary permission
ENV GOOGLE_APPLICATION_CREDENTIALS="/usr/src/app/service_account.json"
Start running your command, docker build and ssh into the container!
# Build the container
docker build -t my-python-app .
# Check that the image is built
docker image ls
# Ssh into the container
docker run -it --rm --name my-running-app my-python-app /bin/bash
Finally, run the python script! 😎
python main.py
You should be able to see your file in the Cloud Storage now!
This is part of my content in my short 1 hour GCP4You series, sign up here to get 1 month of Skillshare for FREE to enjoy my deep-dive session and other topics on how to integrate with GCP. 😎