Deploying a Google Cloud Function Triggered by Cloud Storage Events
📖 Scenario: You work for a company that stores images in Google Cloud Storage. You want to automatically process images whenever a new file is uploaded to a storage bucket.To do this, you will create a Google Cloud Function that triggers when a new file is added to a specific Cloud Storage bucket.
🎯 Goal: Build a Google Cloud Function that triggers on new file uploads to a Cloud Storage bucket named image-uploads. The function will log the file name and bucket name.
📋 What You'll Learn
Create a Cloud Storage bucket named image-uploads.
Create a Cloud Function named log_new_file triggered by new file creation in the image-uploads bucket.
The function should log the file name and bucket name from the event data.
Deploy the function with the correct trigger and runtime.
💡 Why This Matters
🌍 Real World
Automating tasks like image processing or data ingestion when files are uploaded to cloud storage is common in many businesses.
💼 Career
Cloud engineers and developers often create event-driven functions to build scalable, reactive cloud applications.
Progress0 / 4 steps
1
Create the Cloud Storage bucket
Write the gsutil command to create a Cloud Storage bucket named image-uploads in the us-central1 region.
GCP
Hint
Use gsutil mb -l REGION gs://BUCKET_NAME/ to create a bucket.
2
Write the Cloud Function code
Create a Python function named log_new_file that accepts event and context parameters. Inside the function, assign file_name to event['name'] and bucket_name to event['bucket']. Then, write a log statement using print that outputs: New file {file_name} uploaded to {bucket_name}.
GCP
Hint
Use event['name'] and event['bucket'] to get file and bucket names.
3
Configure the Cloud Function trigger
Write the gcloud command to deploy the Cloud Function named log_new_file using Python 3.9 runtime. Set the trigger to activate on new object creation events in the image-uploads bucket.
GCP
Hint
Use --trigger-event google.storage.object.finalize for new file uploads.
4
Verify the Cloud Function trigger configuration
Write the gcloud command to describe the Cloud Function log_new_file and confirm it is triggered by the image-uploads bucket on new object creation events.
GCP
Hint
Use gcloud functions describe FUNCTION_NAME to check configuration.
Practice
(1/5)
1. What is the main purpose of an event triggered function in Google Cloud Functions?
easy
A. To store data permanently in the cloud
B. To manually start a function from the console
C. To schedule functions to run at fixed times
D. To automatically run code when a specific event occurs
Solution
Step 1: Understand event triggered functions
Event triggered functions run automatically when a specific event happens, like a file upload or database change.
Step 2: Compare options with the definition
Only To automatically run code when a specific event occurs describes automatic running on events. Options B and C describe manual or scheduled runs, and D is unrelated.
Final Answer:
To automatically run code when a specific event occurs -> Option D
Quick Check:
Event triggered function = automatic run on event [OK]
Hint: Event triggered means automatic run on event [OK]
Common Mistakes:
Confusing event triggered with scheduled functions
Thinking manual start is event triggered
Mixing storage with function triggers
2. Which of the following is the correct way to specify a Cloud Function triggered by a Cloud Storage event in deployment?
easy
A. gcloud functions deploy myFunc --trigger-resource my-bucket --trigger-event google.storage.object.finalize
B. gcloud functions deploy myFunc --trigger-topic myTopic
C. gcloud functions deploy myFunc --trigger-http
D. gcloud functions deploy myFunc --trigger-schedule "every 5 minutes"
Cloud Storage events use --trigger-resource for the bucket and --trigger-event for the event type like object finalize.
Step 2: Match options with correct syntax
gcloud functions deploy myFunc --trigger-resource my-bucket --trigger-event google.storage.object.finalize correctly uses --trigger-resource and --trigger-event for Cloud Storage. Others are for HTTP, Pub/Sub, or scheduled triggers.
5. You want to create a Cloud Function that triggers when a new file is uploaded to any folder inside a Cloud Storage bucket named 'photos'. Which trigger configuration is correct?
hard
A. Use --trigger-resource photos and --trigger-event google.storage.object.metadataUpdate
B. Use --trigger-resource photos and --trigger-event google.storage.object.finalize
C. Use --trigger-resource photos and --trigger-event google.storage.object.archive
D. Use --trigger-resource photos/* and --trigger-event google.storage.object.delete
Solution
Step 1: Identify correct event for new file upload
The event google.storage.object.finalize triggers when a new object is created or overwritten.
Step 2: Confirm resource and event usage
The resource is the bucket name 'photos'. Wildcards like 'photos/*' are not valid for --trigger-resource.
Final Answer:
Use --trigger-resource photos and --trigger-event google.storage.object.finalize -> Option B
Quick Check:
New file upload = finalize event on bucket resource [OK]
Hint: Use bucket name and finalize event for new uploads [OK]
Common Mistakes:
Using delete or archive events instead of finalize
Trying to use wildcards in trigger-resource
Using metadataUpdate event which triggers on metadata changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.