Quickstart: Receive events from Cloud Storage

This quickstart shows you how to receive events from Cloud Storage in an unauthenticated Cloud Run service using Eventarc, but without using Cloud Audit Logs.

You can configure the triggering of notifications in response to various events inside a Cloud Storage bucket—object creation, deletion, archiving, and metadata updates.

In this quickstart, you:

  1. Create a Cloud Storage bucket to be the event source.

  2. Deploy an event receiver service to Cloud Run.

  3. Create an Eventarc trigger.

  4. Generate an event by uploading a file to the Cloud Storage bucket, and view it in the Cloud Run logs.

Before you begin

Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.

  1. 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.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Cloud Build, Cloud Logging, Cloud Run, Cloud Storage, Eventarc, and Pub/Sub APIs.

    Enable the APIs

  5. Update gcloud components:
    gcloud components update
  6. Log in using your account:
    gcloud auth login
  7. Set the configuration variables used in this quickstart:
    gcloud config set project PROJECT_ID
    gcloud config set run/region us-central1
    gcloud config set run/platform managed
    gcloud config set eventarc/location us-central1
    Replace PROJECT_ID with your Google Cloud project ID. You can find your project ID on the Dashboard page of the Google Cloud Console.
  8. Grant the pubsub.publisher role to the Cloud Storage service account:

    SERVICE_ACCOUNT="$(gsutil kms serviceaccount -p PROJECT_ID)"
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:${SERVICE_ACCOUNT}" \

  9. If you enabled the Pub/Sub service account on or before April 8, 2021, grant the iam.serviceAccountTokenCreator role to the Pub/Sub service account:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com" \

    Replace PROJECT_NUMBER with your Google Cloud project number. You can find your project number on the Dashboard page of the Google Cloud Console.

  10. Download and install the Git source code management tool.

Create a Cloud Storage bucket

Create a Cloud Storage bucket to use as the event source:

gsutil mb -l us-central1 gs://PROJECT_ID-bucket/

After the event source is created, you can deploy the event receiver service on Cloud Run.

Deploy the event receiver service to Cloud Run

Deploy a Cloud Run service that receives and logs events.

  1. Clone the repository and then change to the directory that contains the Cloud Run sample code:


    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/eventarc/audit_storage


    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    cd java-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
    cd dotnet-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    cd nodejs-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    cd python-docs-samples/eventarc/audit-storage
  2. Build the container and upload it to Cloud Build:

     gcloud builds submit --tag gcr.io/PROJECT_ID/helloworld-events
  3. Deploy the container image to Cloud Run:

     gcloud run deploy helloworld-events \
         --image gcr.io/PROJECT_ID/helloworld-events \

    When the deployment succeeds, the command line displays the service URL.

Now that you have deployed a event receiver service called helloworld-events to Cloud Run, you can set up your trigger.

Create an Eventarc trigger

The Eventarc trigger sends events from the Cloud Storage bucket to the helloworld-events Cloud Run service.

  1. Create a trigger that filters Cloud Storage events and that uses the Compute Engine default service account:

     gcloud eventarc triggers create storage-events-trigger \
         --destination-run-service=helloworld-events \
         --destination-run-region=us-central1 \
         --event-filters="type=google.cloud.storage.object.v1.finalized" \
         --event-filters="bucket=PROJECT_ID-bucket" \

    This creates a trigger called storage-events-trigger.

  2. To confirm storage-events-trigger was successfully created, run:

     gcloud eventarc triggers list --location=us-central1

    The output is similar to the following:

     NAME                    TYPE                                      DESTINATION_RUN_SERVICE  DESTINATION_RUN_PATH  ACTIVE
     storage-events-trigger  google.cloud.storage.object.v1.finalized  helloworld-events                              Yes

Generate and view an event

  1. To generate an event:

    Upload a text file to Cloud Storage:

     echo "Hello World" > random.txt
     gsutil cp random.txt gs://PROJECT_ID-bucket/random.txt

    The upload generates an event and the Cloud Run service logs the event's message.

  2. To view the log entry:

    1. Filter the log entries and return the output in JSON format:

      gcloud logging read "resource.labels.service_name=helloworld-events AND textPayload:random.txt" --format=json
    2. Look for a log entry similar to:

      "textPayload": "2021/08/09 19:04:25 Detected change in Cloud Storage bucket: objects/random.txt"

Congratulations! You have successfully deployed an event receiver service to Cloud Run, created an Eventarc trigger, generated an event from Cloud Storage, and viewed it in the Cloud Run logs.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this page, follow these steps.

While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Container Registry, storing files in your Cloud Storage bucket, and Eventarc resources.

You can:

  1. Delete your image.

  2. Delete your storage bucket.

  3. Delete the Eventarc trigger.

Alternatively, you can delete your Google Cloud project to avoid stop billing for all the resources used within that project.

To delete your project:

gcloud projects delete PROJECT_ID_OR_NUMBER

Replace PROJECT_ID_OR_NUMBER with your Google Cloud project ID or number.

What's next