This simple tutorial demonstrates writing, deploying, and triggering an Event-Driven Cloud Run function with a Cloud Storage trigger to respond to Cloud Storage events.
If you're looking for code samples for using Cloud Storage itself, visit the Google Cloud sample browser.
Objectives
- Write and deploy an Event-Driven Cloud Run function.
- Trigger the function by uploading a file to Cloud Storage.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Run functions
- Cloud Storage
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Eventarc APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Eventarc APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Prepare your development environment:
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Preparing the application
Create a Cloud Storage bucket to upload a test file, where
YOUR_TRIGGER_BUCKET_NAME
is a globally unique bucket name:gcloud storage buckets create gs://YOUR_TRIGGER_BUCKET_NAME
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/hello-gcs/
C#
cd dotnet-docs-samples/functions/helloworld/HelloGcs/
Ruby
cd ruby-docs-samples/functions/helloworld/storage/
PHP
cd php-docs-samples/functions/helloworld_storage/
Deploying and triggering the function
Cloud Storage functions are based on Pub/Sub notifications from Cloud Storage and support similar event types:
The following sections describe how to deploy and trigger a function for each of these event types.
Object Finalize
Object finalize events trigger when a "write" of a Cloud Storage Object is successfully finalized. In particular, this means that creating a new object or overwriting an existing object triggers this event. Archive and metadata update operations are ignored by this trigger.
Object Finalize: deploying the function
Take a look at the sample function, which handles Cloud Storage events:
Node.js
Python
Go
Java
C#
Ruby
PHP
To deploy the function, run the following command in the directory where the sample code is located:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
where YOUR_TRIGGER_BUCKET_NAME
is the name of the
Cloud Storage bucket that triggers the function.
Object Finalize: triggering the function
To trigger the function:
Create an empty
gcf-test.txt
file in the directory where the sample code is located.Upload the file to Cloud Storage in order to trigger the function:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
where
YOUR_TRIGGER_BUCKET_NAME
is the name of your Cloud Storage bucket where you will upload a test file.Check the logs to make sure the executions have completed:
gcloud functions logs read --limit 50
Object Delete
Object delete events are most useful for non-versioning buckets. They are triggered when an old version of an object is deleted. In addition, they are triggered when an object is overwritten. Object delete triggers can also be used with versioning buckets, triggering when a version of an object is permanently deleted.
Object Delete: deploying the function
Using the same sample code as in the finalize example, deploy the function with object delete as the trigger event. Run the following command in the directory where the sample code is located:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
where YOUR_TRIGGER_BUCKET_NAME
is the name of the
Cloud Storage bucket that triggers the function.
Object Delete: triggering the function
To trigger the function:
Create an empty
gcf-test.txt
file in the directory where the sample code is located.Make sure that your bucket is non-versioning:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
Upload the file to Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
where
YOUR_TRIGGER_BUCKET_NAME
is the name of your Cloud Storage bucket where you will upload a test file. At this point the function should not execute yet.Delete the file to trigger the function:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
Check the logs to make sure the executions have completed:
gcloud functions logs read --limit 50
Note that the function may take some time to finish executing.
Object Archive
Object archive events can be used only with versioning buckets. They are triggered when an old version of an object is archived. In particular, this means that when an object is overwritten or deleted, an archive event is triggered.
Object Archive: deploying the function
Using the same sample code as in the finalize example, deploy the function with object archive as the trigger event. Run the following command in the directory where the sample code is located:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
where YOUR_TRIGGER_BUCKET_NAME
is the name of the
Cloud Storage bucket that triggers the function.
Object Archive: triggering the function
To trigger the function:
Create an empty
gcf-test.txt
file in the directory where the sample code is located.Make sure that your bucket has versioning enabled:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --versioning
Upload the file to Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
where
YOUR_TRIGGER_BUCKET_NAME
is the name of your Cloud Storage bucket where you will upload a test file. At this point the function should not execute yet.Archive the file to trigger the function:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
Watch the logs to make sure the executions have completed:
gcloud functions logs read --limit 50
Object Metadata Update
Metadata update events are triggered when the metadata of existing object is updated.
Object Metadata Update: deploying the function
Using the same sample code as in the finalize example, deploy the function with metadata update as the trigger event. Run the following command in the directory where the sample code is located:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs20 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-gcs-function \ --entry-point HelloGcs.Function \ --runtime dotnet6 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy hello_gcs --runtime ruby32 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy helloGCS --runtime php82 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
where YOUR_TRIGGER_BUCKET_NAME
is the name of the
Cloud Storage bucket that triggers the function.
Object Metadata Update: triggering the function
To trigger the function:
Create an empty
gcf-test.txt
file in the directory where the sample code is located.Make sure that your bucket is non-versioning:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
Upload the file to Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
where
YOUR_TRIGGER_BUCKET_NAME
is the name of your Cloud Storage bucket where you will upload a test file. At this point the function should not execute yet.Update the metadata of the file:
gcloud storage objects update gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt --content-type=text/plain
Watch the logs to make sure the executions have completed:
gcloud functions logs read --limit 50
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the function
Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.
To delete the function you created in this tutorial, run the following command:
Node.js
gcloud functions delete helloGCS
Python
gcloud functions delete hello_gcs
Go
gcloud functions delete HelloGCS
Java
gcloud functions delete java-gcs-function
C#
gcloud functions delete csharp-gcs-function
Ruby
gcloud functions delete hello_gcs
PHP
gcloud functions delete helloGCS
You can also delete Cloud Run functions from the Google Cloud console.