Quickstart: Receiving Cloud Storage events

This page shows you how to receive events from Cloud Storage in a Cloud Run (fully managed) service using Eventarc.

In this quickstart, you:

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

  2. Deploy an event receiver service to Cloud Run (fully managed).

  3. Create an event trigger.

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

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

  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 Run, Cloud Logging, Cloud Build, Pub/Sub, Eventarc APIs.

    Enable the APIs

  5. Update gcloud components:
    gcloud components update
  6. Install the gcloud command-line tool beta components:

    gcloud components install beta
    

  7. Log in using your account:

    gcloud auth login
    

  8. 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
    

  9. Enable Cloud Audit Logs Admin Read, Data Read, and Data Write Log Types in Cloud Storage:

    Go to the Cloud Audit Logs console

  10. Grant the iam.serviceAccountTokenCreator role to the Pub/Sub service account:

    export PROJECT_NUMBER="$(gcloud projects list --filter=$(gcloud config get-value project) --format='value(PROJECT_NUMBER)')"
    
    gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
        --member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"\
        --role='roles/iam.serviceAccountTokenCreator'
    

  11. Grant the eventarc.admin role to the Compute Engine service acount:

    gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
        --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
        --role='roles/eventarc.admin'
    

  12. Download and install the git source code management tool.

Creating a Cloud Storage bucket

This quickstart uses Cloud Storage as the event source. To create a storage bucket:

gsutil mb -l us-central1 gs://events-quickstart-$(gcloud config get-value project)/

Once the event source is created, you can deploy the event receiver service on Cloud Run (fully managed).

Deploying the event receiver service to Cloud Run (fully managed)

Deploy a Cloud Run (fully managed) service that receives and logs events. To deploy the sample event receiver service:

  1. Clone the repository:

    Python

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

    Java

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

    Go

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

    Node.js

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

    C#

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

    gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/helloworld-events
    
  3. Deploy the container image to Cloud Run (fully managed):

    gcloud run deploy helloworld-events \
        --image gcr.io/$(gcloud config get-value project)/helloworld-events \
        --allow-unauthenticated
    

    The service URL will be displayed on the command line when it is successfully deployed.

Cloud Run locations

Cloud Run is regional, which means the infrastructure that runs your Cloud Run services is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your Cloud Run services are run. You can generally select the region nearest to your users but you should consider the location of the other Google Cloud products that are used by your Cloud Run service. Using Google Cloud products together across multiple locations can affect your service's latency as well as cost.

Cloud Run is available in the following regions:

Subject to Tier 1 pricing

  • asia-east1 (Taiwan)
  • asia-northeast1 (Tokyo)
  • asia-northeast2 (Osaka)
  • europe-north1 (Finland)
  • europe-west1 (Belgium)
  • europe-west4 (Netherlands)
  • us-central1 (Iowa)
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • us-west1 (Oregon)

Subject to Tier 2 pricing

  • asia-east2 (Hong Kong)
  • asia-northeast3 (Seoul, South Korea)
  • asia-southeast1 (Singapore)
  • asia-southeast2 (Jakarta)
  • asia-south1 (Mumbai, India)
  • australia-southeast1 (Sydney)
  • europe-west2 (London, UK)
  • europe-west3 (Frankfurt, Germany)
  • europe-west6 (Zurich, Switzerland)
  • northamerica-northeast1 (Montreal)
  • southamerica-east1 (Sao Paulo, Brazil)

If you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Cloud Console.

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

Creating an Eventarc trigger

The Eventarc trigger will send events from the Cloud Storage bucket to the helloworld-events Cloud Run (fully managed) service.

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

    gcloud beta eventarc triggers create events-quickstart-trigger \
        --destination-run-service=helloworld-events \
        --destination-run-region=us-central1 \
        --matching-criteria="type=google.cloud.audit.log.v1.written" \
        --matching-criteria="serviceName=storage.googleapis.com" \
        --matching-criteria="methodName=storage.objects.create" \
        --service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com
    

    This creates a trigger called events-quickstart-trigger.

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

    gcloud beta eventarc triggers list --location=us-central1
    

    You should see events-quickstart-trigger listed with a target of helloworld-events.

Generating and viewing an event

To generate an event:

  1. Upload a text file to Cloud Storage:

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt
    

    The upload generates an event and the Cloud Run (fully managed) service logs the event's message.

  2. View the log entry:

    gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=helloworld-events"
    
  3. Look for a log entry similar to:

    Detected change in GCS bucket: storage.googleapis.com/projects/_/buckets/BUCKET_NAME/objects/random.txt
    

    Where BUCKET_NAME is the name of the Cloud Storage bucket.

Clean up

Removing your test project

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, Eventarc resources, and for storing files in your Cloud Storage bucket.

You can delete your image, delete your storage bucket. To delete the Eventarc trigger:

gcloud beta eventarc triggers delete events-quickstart-trigger

Alternatively, you can delete your Cloud project to avoid incurring charges. Deleting your Cloud project stops billing for all the resources used within that project.

  1. In the Cloud Console, go to the Manage resources page.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete and then click Delete .
  3. In the dialog, type the project ID and then click Shut down to delete the project.

What's next