DEV Community

Cover image for Part-47: Trigger a Cloud Function based on the Cloud Storage Events in GCP
Latchu@DevOps
Latchu@DevOps

Posted on

Part-47: Trigger a Cloud Function based on the Cloud Storage Events in GCP

☁️ Cloud Functions – Cloud Storage Trigger

Google Cloud Functions integrates with Cloud Storage to automatically execute code when objects are created, updated, or deleted. This is powerful for automation, image/video processing, logging, and pipelines. 🚀


✅ Step-01: Introduction

We’ll create:

  1. A Cloud Storage Bucket
  2. A Cloud Function with a Cloud Storage Event Trigger
  3. Upload files and verify logs

✅ Step-02: Create Cloud Storage Bucket

  • Navigate to Cloud Storage → CREATE
  • Enter: Bucket name: mycfdemobucket1021 (must be globally unique)
  • Leave all other options as default
  • Click CREATE

b1


✅ Step-03: Create Cloud Function with Storage Trigger

Configuration Tab

  • Function name: cf-demo3-events-storage
  • Region: us-central1
  • Trigger: Cloud Storage
  • Event Type: google.cloud.storage.object.v1.finalized (Object created/updated)
  • Bucket: mycfdemobucket1021
  • Leave all defaults → Click NEXT

b2

b3

Code Tab

  • Runtime: Node.js 22
  • Use the auto-generated code (below)
  • Click Save and DEPLOY

b4

const functions = require('@google-cloud/functions-framework');

// Register a CloudEvent callback with the Functions Framework that will
// be triggered by Cloud Storage.
functions.cloudEvent('helloGCS', cloudEvent => {
  console.log(`Event ID: ${cloudEvent.id}`);
  console.log(`Event Type: ${cloudEvent.type}`);

  const file = cloudEvent.data;
  console.log(`Bucket: ${file.bucket}`);
  console.log(`File: ${file.name}`);
  console.log(`Metageneration: ${file.metageneration}`);
  console.log(`Created: ${file.timeCreated}`);
  console.log(`Updated: ${file.updated}`);
});
Enter fullscreen mode Exit fullscreen mode

✅ Step-04: Review Cloud Function Logs

  • Go to: Cloud Functions → cf-demo3-events-storage → Logs
  • You’ll see logs once a file is uploaded or updated.

✅ Step-05: Upload a New File

  • Navigate to Cloud Storage → Upload file → Select myfile1.txt

b5

  • Go to Cloud Function Logs → Verify file event logs

b6


🎉 That’s it! You’ve successfully created a Cloud Function with a Cloud Storage Event Trigger. Every time a file is uploaded/updated, the function will log details such as bucket name, file name, and timestamps.

Top comments (0)