Partha Bhattacharya
3 min readOct 4, 2018

--

Image processing using Google Cloud function

Google Cloud Function (GCF) is a Serverless service. Serverless service means we do not need to manage where and how we deploy it. We do not have to manage the underlying servers and its resources. We will solely focus on our business requirement and deliver it as fast as possible.

This article shows how to process an image from Google Storage Bucket. Our goal is to create predefined thumb image of various size for images uploaded in Google Cloud Storage. As soon as an image file is uploaded a function will be automatically triggered and predefined thumb images will be created. The generated thumb images will be copied in a predefined storage bucket.

We need;

  1. A Google Cloud Platform(GCP) account
  2. Storage Bucket
  3. Google Cloud Function (GCF) enabled.
  4. And some knowledge of node

Signup to your Google cloud console. Create two storage bucket upload-image (uploaded images will be stored here) and thumb-image (generated thumb images will be stored here). Next we have to enable Google Cloud Function API.

We are ready to start writing our function. We will use new node8 version to write our function. There are two ways we can write our function. Using Google console or using any of our favourite editor. I prefer to use visual studio code. Since we will write our function locally using our favourite editor, we will require node 8 installed.

Create a folder and fire-up vidual studio code editor

$ mkdir gcf-thumb-img
$ cd gcf-thumb-img
$ code .

Install required node packages

$ npm i @google-cloud/storage
$ npm i sharp
$ npm i fs-extra

Sharp is a image converter library which is very fast. fs-extra module adds promise support to the fs methods.

Process

  1. On successful file creation in upload-image bucket our GCF will be triggered.
  2. Check if the file is a valid image.
  3. Download image in a temporary working directory
  4. Use sharp library to convert image and save it in a temporary location
  5. Upload the created thumb image to bucket thumb-image

Complete code:

"use strict";const {Storage} = require("@google-cloud/storage");
const sharp = require('sharp');
const path = require('path');
const fs = require('fs-extra');
const os = require('os');
//destination bucket
const bucketName = {
dst: 'thumb-image'
};
const storage = new Storage();exports.processThumbImage = async (data, context) => {const file = data;// 1. Check for image file
if (!file.contentType.includes('image')) {
console.log('exiting function');
return false;
}
const scrBucket = storage.bucket(file.bucket);
const dstBucket = storage.bucket(bucketName.dst);
const workingDir = path.join(os.tmpdir(), 'thumbs');
const tmpFilePath = path.join(workingDir, file.name);
// 2. Wait for temp directory to be ready
await fs.ensureDir(workingDir);
// 3. Download file to temp location
await scrBucket.file(file.name).download({
destination: tmpFilePath
});
const sizes = [800,512,256,128];const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${file.name}`;
const thumbPath = path.join(workingDir, thumbName);
// 4. Create thumb image
await sharp(tmpFilePath)
.resize(size)
.toFile(thumbPath);
// 5. Upload thumb image
return dstBucket.upload(thumbPath, {
metadata: {
contentType: 'image/png',
cacheControl : 'public,max-age=3600',
}
});
});await Promise.all(uploadPromises);// 6. Remove temp directory
await fs.remove(workingDir);
return true;
};

Deploy the function using glcoud command which is part of Google cloud SDK

$ gcloud beta functions deploy processThumbImage \
--runtime nodejs8 \
--region [YOUR-REGION] \
--project [YOUR-PROJECT-NAME] \
--trigger-resource upload-image \
--trigger-event google.storage.object.finalize

To test our GCF function upload an image file into upload-image bucket. Now open thumb-image bucket to view the generated images. It might take few seconds for new generated thumb images to appear. In case of any error please check GCF log from Google console.

--

--