Receive direct events from Cloud Storage (Google Cloud console)

This quickstart shows you how to receive direct events from Cloud Storage in an unauthenticated Cloud Run service.

You can complete this quickstart using the Google Cloud console. For instructions using the Google Cloud CLI, see Receive events using Cloud Audit Logs (gcloud CLI).

In this quickstart, you:

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

  2. Deploy an event receiver service to Cloud Run.

  3. Create an Eventarc trigger to filter and route events.

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


For step-by-step guidance on this task directly in Google Cloud console, click Guide me:

Guide me


The following sections take you through the same steps as clicking Guide me.

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 check if billing is enabled on a project.

  4. Enable the Cloud Build and Eventarc APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  7. Enable the Cloud Build and Eventarc APIs.

    Enable the APIs

Create a Cloud Storage bucket

This quickstart uses Cloud Storage as the event source.

  1. In the Google Cloud console, go to Cloud Storage.

    Go to Cloud Storage

  2. Click Create bucket.

    New bucket.

  3. Enter your bucket information and click Continue to complete each step:

    1. Enter a unique Name. For example, helloworld-bucket.

    2. For Location type, select Region.

    3. In the Location list, select us-central1 (Iowa).

    4. Accept the other defaults.

  4. Click Create.

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

Deploy the event receiver service to Cloud Run

Deploy a Cloud Run service that receives and logs events.

  1. Fork the sample repository to your GitHub account:

    Go

    1. On GitHub, navigate to GoogleCloudPlatform/golang-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

    Java

    1. On GitHub, navigate to GoogleCloudPlatform/java-docs-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

    .NET

    1. On GitHub, navigate to GoogleCloudPlatform/dotnet-docs-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

    Node.js

    1. On GitHub, navigate to GoogleCloudPlatform/nodejs-docs-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

    Python

    1. On GitHub, navigate to GoogleCloudPlatform/python-docs-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

  2. In the Google Cloud console, go to Cloud Run.

    Go to Cloud Run

  3. Click Create service.

  4. In the Create service form, select Continuously deploy new revisions from a source repository.

  5. Click Set up with Cloud Build.

  6. In the Set up with Cloud Build form, complete the following steps:

    Set up with Cloud Build page

    1. If prompted, enable the Cloud Build API.
    2. In the Repository Provider list, select GitHub.

    3. In the Repository list, select the GitHub repository you forked.

    4. Accept the legal agreement and click Next.

    5. Leave the Branch field as ^main$.

    6. For the Build Type, select Dockerfile and provide the source location of the Dockerfile:

      eventarc/audit-storage/Dockerfile
      

      Or, for the Go sample:

      eventarc/audit_storage/Dockerfile
      
    7. Click Save.

  7. In the Create service form, type a service name. For example, helloworld-events.

  8. In the Region list, leave us-central1(Iowa) as your service location.

  9. Accept the CPU allocation and pricing and Autoscaling defaults.

  10. In the Ingress section, select Allow all traffic.

  11. In the Authentication section, select Allow unauthenticated invocations.

  12. Click Create.

Create an Eventarc trigger

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

  1. In the Google Cloud console, go to Eventarc.

    Go to Eventarc

  2. Click Create trigger.

  3. Type a Trigger name.

    For example, helloworld-trigger.

  4. In the Event provider list, select Google sources > Cloud Storage.

  5. In the Event list, select Direct > google.cloud.storage.object.v1.finalized.

  6. For Bucket, specify the helloworld-bucket Cloud Storage bucket you created previously.

  7. If prompted, grant the following:

    • iam.serviceAccountTokenCreator role to Pub/Sub service account
    • pubsub.publisher role to Cloud Storage service account
  8. Use the Default compute service account as the service account that invokes your service.

  9. In the Event destination list, select Cloud Run.

  10. For the Cloud Run service, select the helloworld-events service you created previously.

  11. Click Create.

Generate and view an event

Generate an event and confirm that the Eventarc trigger is working as expected.

  1. To generate an event:

    1. Create a text file with the file name random.txt and the text "Hello World".

    2. In the Google Cloud console, go to Cloud Storage.

      Go to Cloud Storage

    3. Click the name of the storage bucket you created.

    4. In the Objects tab, click Upload files and upload the random.txt file.

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

  2. To view the log entry:

    1. In the Google Cloud console, go to Cloud Run.

      Go to Cloud Run

    2. Click the name of the service you created.

    3. To retrieve the log entries for all revisions of this service, in the Service details page, click the Logs tab. You can filter by log severity level.

    4. Look for a log entry similar to:

      Received event of type google.cloud.storage.object.v1.finalized. Event data: [...]

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

You can delete your image, delete your storage bucket, and delete your service.

To delete the Eventarc trigger:

  1. In the Google Cloud console, go to Eventarc.

    Go to Eventarc

  2. Click the name of the trigger you created.

  3. On the Triggers details page, click Delete.

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

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  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