Create a simple event-driven Google Cloud Function app to create an image thumbnail

Sandeep Pamidamarri
4 min readOct 8, 2020

--

In this particular post, we will learn to create a simple event-driven google cloud function that creates an image thumbnail for the uploaded image. Google cloud function is a FaaS service that has zero server management, pay only for the usage.

Following technologies and systems are used:

  1. Python — To develop the function code
  2. Google Cloud Function
  3. Google Cloud Storage

Create the google cloud storage buckets

Step 1: Create two google cloud storage buckets

Refer to the below documentation to create the storage buckets

Images bucket:

Thumbnails bucket:

Develop the Python Function to Make Thumbnail

Step 1: Set-up Python project using PyCharm IDE

PyCharm is a useful IDE tool for Python project development. Refer to the below website on the download instructions

Step 2: Create a new project using PyCharm

Create the new project [MakeThumbnail] in PyCharm IDE. The project structure looks as below

Note: We do create the additional files as part of the process

Step 3: Install the Python packages

Create the [requirements.txt] file for the required packages. For this function we need the following packages:

Wand: To handle with images

google-cloud-storage: To communicate with google cloud storage services

wand
google-cloud-storage

Step 4: Develop the function code

Create the file [main.py] file and copy the following content. The function logic is self-explanatory.

from json import load as json_load
from wand.image import Image
from google.cloud import storage

with open('config.json') as json_data_file:
cfg = json_load(json_data_file)

client = storage.Client()

def make_thumbnail(data, context):
# Get the image from GCS
bucket = client.get_bucket(data['bucket'])
blob = bucket.get_blob(data['name'])
imagedata = blob.download_as_string()

# Create a new image object and resample it
newimage = Image(blob=imagedata)
newimage.sample(300,300)

# Upload the resampled file to the thumbnails bucket
bucket = client.get_bucket(cfg['THUMBNAIL_BUCKET'])
newblob = bucket.blob('thumbnail-'+data['name'])
newblob.upload_from_string(newimage.make_blob())

Create the [config.json] file

{
"THUMBNAIL_BUCKET": "THUMBNAILS-BUCKETNAME"
}

Deploy function to Google Cloud

Step 1: Set-up google cloud account and create a project

Refer to the google cloud documentation on the cloud account and respective project creation

Step 2: Install Google Cloud SDK Shell

Refer to the below documentation and install the Google Cloud SDK

Step 3: Create the Google cloud Ignore file

The “.gcloudignore” file is used to skip the assets not required to move to the google cloud.

Example:

# Virtual environment folder
venv/

# Ignore the Sample folders
Sample Pics/

# Ignore deploy.sh file
deploy.sh

Step 4: Deploy using google cloud SDK

Create the “deploy.sh” file with the following command to deploy.

Note: Google cloud function will get invoked with the storage bucket event on finalizing the file

gcloud functions deploy make_thumbnail --region REGION_NAME --runtime python37 --trigger-resource IMAGES_BUCKET_NAME --trigger-event google.storage.object.finalize --ignore-file .gcloudignore --allow-unauthenticated

Note: Execute the deploy.sh file from terminal to deploy the function

Step 5: Verify the deployed function in the cloud console

Test the Google Cloud Services

Step 1: Upload a file to the Google Storage Images bucket

Upload any image file with the higher dimensions into images bucket

Step 2: Verify the thumbnail in the Google Storage Thumbnails bucket

Verify the respective thumbnail image in the thumbnails bucket. The thumbnail image will be in the dimensions of [300X300].

Wohoooo, congratulations :) Now you successfully created a simple event-driven google cloud function. The event-driven architecture is used to handle the asynchronous activities of an application. This approach is completely serverless and has the freedom to write in any language.

--

--

Sandeep Pamidamarri
Sandeep Pamidamarri

Written by Sandeep Pamidamarri

Digital Transformation Leader | Pega Lead Solution Architect | Pega Certified Data Scientist | Pega Customer Service | Pega Sales Automation | AWS Cloud

No responses yet