Open In App

How To Trigger Something When Object is Added in Google Cloud Storage Bucket?

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Large amounts of data, including pictures, videos, and other files, can be stored and retrieved using the popular object storage service known as Google Cloud Storage. When an object is added, edited, or removed from a bucket, you can set off events. This is one of Google Cloud Storage’s core features. This demonstrates how to use Google Cloud Functions to start an activity when an object is added to a bucket.

Example:

Consider having a Google Cloud Storage bucket where people may add photographs. When these pictures are added to a specified folder in the bucket, you want to resize them automatically. You may achieve this by using a Cloud Function that runs whenever a new object is added to the folder. The image will then be scaled by the cloud function, which will save the resized version to a new folder in the same bucket.

Code

The following Node.js code can be used to put this example into practice. The Cloud Function defined by this code is activated whenever a new object is added to a particular folder in a Google Cloud Storage bucket.

Javascript




const {Storage} = require('@google-cloud/storage');
const sharp = require('sharp');
  
exports.resizeImage = async (event, context) => {
  const file = event.data;
  const bucketName = file.bucket;
  const fileName = file.name;
  
  // Check if the file is uploaded to the correct folder
  if (!fileName.startsWith('uploads/')) {
    console.log(`File ${fileName} is not uploaded to the uploads folder.`);
    return;
  }
  
  // Initialize the Google Cloud Storage client
  const storage = new Storage();
  
  // Get the source file from the bucket
  const bucket = storage.bucket(bucketName);
  const sourceFile = bucket.file(fileName);
  
  // Define the destination file path and file name
  const destFilePath = fileName.replace('uploads/', 'resized/');
  const destFile = bucket.file(destFilePath);
  
  // Resize the image using Sharp
  const buffer = await sourceFile.download();
  const resizedBuffer = await sharp(buffer[0])
    .resize(500, 500)
    .toBuffer();
  
  // Upload the resized image to the bucket
  await destFile.save(resizedBuffer);
  
  console.log(`File ${fileName} was successfully resized and 
  saved to ${destFilePath}.`);
};


The above code creates a cloud function that watches for the ‘google.storage.object.finalize’ event in a Google Cloud Storage bucket. The function resizes any image file that is uploaded to the bucket’s ‘uploads/’ folder and stores the resized version of the image in the’resized/’ folder.

If the file was successfully resized and saved to the’resized/’ folder, the Cloud Function will log a message to the console when it receives a call from the ‘google.storage.object.finalize’ event.

As an example, if you upload the image file ‘image.jpg’ to the bucket’s ‘uploads/’ folder, the cloud function will resize it and save it as’resized/image.jpg’ in the’resized/’ folder.

Output:

Here is an illustration of the output from the Cloud Function logs:

File uploads/image.jpg was successfully resized and saved to resized/image.jpg.

Google Cloud Functions make it simple to start processes when items are added to, edited in, or removed from a Google Cloud Storage bucket. We showed how to resize photographs in this example when they are uploaded to a certain folder in the bucket. This method can be used to automate a variety of different activities, including creating thumbnails, transcoding videos, and using machine learning models to analyse data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads