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.
Visit the Google Cloud sample browser if you're looking for code samples for using Cloud Storage.
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 Build
- Pub/Sub
- Cloud Storage
- Artifact Registry
- Eventarc
- Cloud Logging
For details, see Cloud Run functions pricing.
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, Artifact Registry, Eventarc, Logging, and Pub/Sub 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, Artifact Registry, Eventarc, Logging, and Pub/Sub 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
Prerequisites
Create a regional bucket, where
YOUR_BUCKET_NAME
is a globally unique bucket name, andREGION
is the region in which you plan to deploy your function:gcloud storage buckets create gs://YOUR_BUCKET_NAME --location=REGION
To use Cloud Storage functions, grant the
pubsub.publisher
role to the Cloud Storage service account:PROJECT_ID=$(gcloud config get-value project) PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)') SERVICE_ACCOUNT=$(gcloud storage service-agent --project=$PROJECT_ID) gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$SERVICE_ACCOUNT \ --role roles/pubsub.publisher
Prepare the application
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/v2/helloGCS/
Python
cd python-docs-samples/functions/v2/storage/
Go
cd golang-samples/functions/functionsv2/hellostorage/
Java
cd java-docs-samples/functions/v2/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/
Deploy and trigger 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: deploy 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 nodejs-finalize-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy python-finalize-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy go-finalize-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloStorage \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-finalize-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloGcs \ --memory=512MB \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-finalize-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloGcs.Function \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy ruby-finalize-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy php-finalize-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
Replace the following:
- REGION: The name of the
Google Cloud region where you want to deploy your function
(for example,
us-west1
). - YOUR_BUCKET_NAME: The name of the Cloud Storage bucket that
triggers the function. When deploying Cloud Run functions, specify the bucket
name alone without the leading
gs://
; for example,--trigger-event-filters="bucket=my-bucket"
.
Object Finalize: trigger the function
Test the function by uploading a file to your bucket:
echo "Hello World" > test-finalize.txt gcloud storage cp test-finalize.txt gs://YOUR_BUCKET_NAME/test-finalize.txt
You should see the received CloudEvent in the logs:
gcloud functions logs read YOUR_FUNCTION_NAME --region REGION --gen2 --limit=10
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: deploy 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 nodejs-delete-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy python-delete-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy go-delete-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloStorage \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-delete-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloGcs \ --memory=512MB \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-delete-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloGcs.Function \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy ruby-delete-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy php-delete-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.deleted" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
Replace the following:
- REGION: The name of the
Google Cloud region where you want to deploy your function
(for example,
us-west1
). - YOUR_BUCKET_NAME: The name of the Cloud Storage bucket that
triggers the function. When deploying Cloud Run functions, specify the bucket
name alone without the leading
gs://
; for example,--trigger-event-filters="bucket=my-bucket"
.
Object Delete: trigger the function
To trigger the function:
Create an empty
test-delete.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_BUCKET_NAME --no-versioning
Upload the file to Cloud Storage:
gcloud storage cp test-delete.txt gs://YOUR_BUCKET_NAME
where
YOUR_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_BUCKET_NAME/test-delete.txt
You should see the received CloudEvent in the logs:
gcloud functions logs read YOUR_FUNCTION_NAME --region REGION --gen2 --limit=10
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 earlier 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: deploy 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 nodejs-archive-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy python-archive-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy go-archive-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloStorage \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-archive-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloGcs \ --memory=512MB \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-archive-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloGcs.Function \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy ruby-archive-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy php-archive-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.archived" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
Replace the following:
- REGION: The name of the
Google Cloud region where you want to deploy your function
(for example,
us-west1
). - YOUR_BUCKET_NAME: The name of the Cloud Storage bucket that
triggers the function. When deploying Cloud Run functions, specify the bucket
name alone without the leading
gs://
; for example,--trigger-event-filters="bucket=my-bucket"
.
Object Archive: trigger the function
To trigger the function:
Create an empty
test-archive.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_BUCKET_NAME --versioning
Upload the file to Cloud Storage:
gcloud storage cp test-archive.txt gs://YOUR_BUCKET_NAME
where
YOUR_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_BUCKET_NAME/test-archive.txt
You should see the received CloudEvent in the logs:
gcloud functions logs read YOUR_FUNCTION_NAME --region REGION --gen2 --limit=10
Object Metadata Update
Metadata update events are triggered when the metadata of existing object is updated.
Object Metadata Update: deploy 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 nodejs-metadata-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Node.js version to run
your function.
Python
gcloud functions deploy python-metadata-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Python version to run
your function.
Go
gcloud functions deploy go-metadata-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloStorage \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Go version to run
your function.
Java
gcloud functions deploy java-metadata-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloGcs \ --memory=512MB \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Java version to run
your function.
C#
gcloud functions deploy csharp-metadata-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloGcs.Function \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported .NET version to run
your function.
Ruby
gcloud functions deploy ruby-metadata-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported Ruby version to run
your function.
PHP
gcloud functions deploy php-metadata-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGCS \
--trigger-event-filters="type=google.cloud.storage.object.v1.metadataUpdated" \
--trigger-event-filters="bucket=YOUR_BUCKET_NAME"
Use the --runtime
flag to specify the runtime ID of a
supported PHP version to run
your function.
Replace the following:
- REGION: The name of the
Google Cloud region where you want to deploy your function
(for example,
us-west1
). - YOUR_BUCKET_NAME: The name of the Cloud Storage bucket that
triggers the function. When deploying Cloud Run functions, specify the bucket
name alone without the leading
gs://
; for example,--trigger-event-filters="bucket=my-bucket"
.
Object Metadata Update: trigger the function
To trigger the function:
Create an empty
test-metadata.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_BUCKET_NAME --no-versioning
Upload the file to Cloud Storage:
gcloud storage cp test-metadata.txt gs://YOUR_BUCKET_NAME
where
YOUR_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_BUCKET_NAME/test-metadata.txt --content-type=text/plain
You should see the received CloudEvent in the logs:
gcloud functions logs read YOUR_FUNCTION_NAME --region REGION --gen2 --limit=10
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.
Delete 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.
Delete the function
Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.
To delete the Cloud Run functions you created in this tutorial, run the following command:
gcloud functions delete YOUR_FUNCTION_NAME --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console.