In Cloud Run, you can trigger services from Cloud Storage using Eventarc,
in response to changes in [Cloud Storage](https://docs.cloud.google.com/storage/docs.md.txt).

When you specify a Cloud Storage trigger for your service, you choose an event
type and specify a Cloud Storage bucket. As a result of this trigger,
your service is called whenever a change occurs on an object (file) within the specified bucket.

For your service to be triggered by an event on a Cloud Storage bucket, the
service and the bucket must be in the same Google Cloud project.

For detailed instructions and code samples for creating triggers from Cloud Storage events, refer to the following tutorials:

- [Trigger services from Cloud Storage using Eventarc](https://docs.cloud.google.com/run/docs/tutorials/eventarc.md.txt)

- [Trigger functions from Cloud Storage using Eventarc](https://docs.cloud.google.com/run/docs/tutorials/trigger-functions-storage.md.txt). You can also find sample code in the Google Cloud console that demonstrates how to trigger a function in response to Cloud Storage events, after [deploying your Cloud Run function with a Cloud Storage trigger](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt).

## Supported Cloud Storage event types

Cloud Run supports the following Cloud Storage event types:

| Event | Event type | Description |
|---|---|---|
| *Object finalized* | - `google.cloud.storage.object.v1.finalized` (through Eventarc) | Occurs when you create a new object, or overwrite an existing object, and Cloud Storage creates a new generation of that object. |
| *Object deleted* | - `google.cloud.storage.object.v1.deleted` (through Eventarc) | Occurs when an object is soft deleted. For more information, see [Soft delete](https://docs.cloud.google.com/storage/docs/soft-delete.md.txt). |
| *Object archived* | - `google.cloud.storage.object.v1.archived` (through Eventarc) | Occurs when a live version of an object becomes a noncurrent version. For more information, see [Object Versioning](https://docs.cloud.google.com/storage/docs/object-versioning.md.txt). |
| *Object metadata updated* | - `google.cloud.storage.object.v1.metadataUpdated` (through Eventarc) | Occurs when you change the [metadata](https://docs.cloud.google.com/storage/docs/metadata.md.txt) of an existing object. |

The
[Google Events](https://github.com/googleapis/google-cloudevents)
repository contains additional resources for working with event data.

## Before you begin

1. Make sure you have set up a new project for Cloud Run as described
   in the [setup](https://docs.cloud.google.com/run/docs/setup.md.txt) page.

2. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, Eventarc, Cloud Logging, Pub/Sub, and Cloud Storage APIs:


   [Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=artifactregistry.googleapis.com,cloudbuild.googleapis.com,run.googleapis.com,eventarc.googleapis.com,logging.googleapis.com,storage.googleapis.com,pubsub.googleapis.com)

   <br />

3. [Grant the required IAM roles and permissions](https://docs.cloud.google.com/eventarc/docs/roles-permissions.md.txt).

#### Required roles for the deployer account


To get the permissions that
you need to trigger from Cloud Storage events,

ask your administrator to grant you the
following IAM roles on your project:

- [Cloud Run Source Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.sourceDeveloper) (`roles/run.sourceDeveloper`)
- [Logs View Accessor](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewAccessor) (`roles/logging.viewAccessor`)
- [Project IAM Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/resourcemanager.md.txt#resourcemanager.projectIamAdmin) (`roles/resourcemanager.projectIamAdmin`)
- [Service Account Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountAdmin) (`roles/iam.serviceAccountAdmin`)
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`)
- [Service Usage Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageAdmin) (`roles/serviceusage.serviceUsageAdmin`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

Note that by default,
[Cloud Build
permissions include permissions to upload and download Artifact Registry
artifacts](https://docs.cloud.google.com/artifact-registry/docs/access-control#gcp).

<br />

## Create a trigger for services

After deploying a service, you can configure a trigger using the Google Cloud console,
Google Cloud CLI, or Terraform.

### Console

1. Deploy your Cloud Run service using
   [containers](https://docs.cloud.google.com/run/docs/deploying.md.txt) or from
   [source](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt).

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


   [Go to Cloud Run](https://console.cloud.google.com/run/services)

   <br />

3. From the list of services, click an existing service.

4. On the Service details page, navigate to the **Triggers** tab.

5. Click
   **Add trigger** , and select **Cloud Storage trigger**.

6. In the **Eventarc trigger** pane, modify the
   trigger details as follows:

   1. In the **Trigger name** field, enter a name for the trigger, or use
      the default name.

   2. Select a **Trigger type** from the list to specify one of
      the following trigger types:

      - **Google Sources** to specify triggers for
        Pub/Sub, Cloud Storage, Firestore,
        and other Google event providers.

      - **Third-party** to integrate with non-Google providers
        that offer an Eventarc source. For more information,
        see [Third-party events in Eventarc](https://docs.cloud.google.com/eventarc/docs/third-parties/third-parties-overview).

   3. Select **Cloud Storage** from the **Event provider** list, to
      select a product that provides the type of event for triggering your
      service. For the list of event providers, see
      [Event providers and destinations](https://docs.cloud.google.com/eventarc/docs/event-providers-targets.md.txt).

   4. Select **google.cloud.storage.object.v1.finalized** from the **Event type** list. Your
      trigger configuration varies depending on the supported event type.
      For more information, see [Event types](https://docs.cloud.google.com/eventarc/docs/event-types.md.txt).

   5. In the **Bucket** field, click **Browse** to select a
      Cloud Storage bucket for the trigger to monitor. Changes to objects within this bucket will trigger calls to your function.

   6. If the **Region** field is enabled, select a [location](https://docs.cloud.google.com/eventarc/docs/overview.md.txt#trigger-location)
      for the Eventarc
      trigger. In general, the location of an Eventarc
      trigger should match the location of the Google Cloud resource that
      you want to monitor for events. In most scenarios, you should also
      deploy your service in the same region. See
      [Understand Eventarc locations](https://docs.cloud.google.com/eventarc/docs/understand-locations.md.txt)
      for more details about Eventarc trigger locations.

   7. In the **Service account** field, select a service account.
      Eventarc triggers are linked to service accounts to
      use as an identity when invoking your service. Your
      Eventarc trigger's service account must have the
      permission to invoke your service. By default, Cloud Run
      uses the [Compute Engine default service account](https://docs.cloud.google.com/compute/docs/access/service-accounts.md.txt#default_service_account).

   8. Optionally, specify the **Service URL path** to send the incoming
      request to. This is the relative path on the destination service to
      which the events for the trigger should be sent. For example:
      `/`, `/route`, `route`, and `route/subroute`.

   9. Optionally, to enable retries if the delivery attempt fails, select
      the **Enable retry on failure** checkbox; otherwise, the default
      behavior is a single delivery attempt with no retries. For more
      information, see [Retry events](https://docs.cloud.google.com/eventarc/docs/retry-events.md.txt).

   10. Once you've completed the required fields, click **Save trigger**.

7. After creating the trigger, verify its health by ensuring that
   there is a checkmark

   on the **Triggers** tab.

   > [!NOTE]
   > **Note:** Although a trigger is created immediately, it can take up to two minutes for a trigger to propagate and filter events.

### gcloud

1. Deploy your Cloud Run service using
   [containers](https://docs.cloud.google.com/run/docs/deploying.md.txt) or from
   [source](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt).

2. Run the following command to create a trigger that filters and routes
   events:


       gcloud eventarc triggers create TRIGGER_NAME  \
           --location=LOCATION \
           --destination-run-service=DESTINATION_RUN_SERVICE  \
           --destination-run-region=DESTINATION_RUN_REGION \
           --event-filters="type=EVENT_FILTER_TYPE" \
           --event-filters="bucket=BUCKET" \
           --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

   <br />

   Replace the following:
   - `TRIGGER_NAME`: the ID of the trigger or a fully qualified identifier.
   - `LOCATION`: the location of the Eventarc trigger. Alternatively, you can set the `eventarc/location` property; for example, `gcloud config set eventarc/location us-central1`.

     To avoid any performance and data residency issues, the location must
     match the location of the Google Cloud service that is generating
     events. For more information, see
     [Eventarc locations](https://docs.cloud.google.com/eventarc/docs/locations.md.txt).
   - `DESTINATION_RUN_SERVICE`: the name of the Cloud Run service that receives the events for the trigger. The service can be in any of the Cloud Run supported locations and doesn't need to be in the same location as the trigger. However, the service must be in the same project as the trigger and will receive events as HTTP POST requests sent to its root URL path (`/`), whenever the event is generated.
   - `DESTINATION_RUN_REGION`: (optional) the [Cloud Run location](https://docs.cloud.google.com/run/docs/locations.md.txt) in which the destination Cloud Run service can be found. If not specified, it is assumed that the service is in the same region as the trigger.
   - `EVENT_FILTER_TYPE`: the identifier of the event. An event is generated when an API call for the method succeeds. For long-running operations, the event is only generated at the end of the operation, and only if the action is performed successfully. For a list of supported event types, see [Google event types supported by Eventarc](https://docs.cloud.google.com/eventarc/docs/reference/supported-events.md.txt#directly-from-a-google-cloud-source).
   - `BUCKET`: the globally unique identifier of the Cloud Storage bucket.
   - `SERVICE_ACCOUNT_NAME`: the name of your user-managed service account.
   - `PROJECT_ID`: your Google Cloud project ID.

   Notes:
   - The `--event-filters="type=EVENT_FILTER_TYPE"` flag is required. If no other event filter is set, events for all resources are matched.
   - `EVENT_FILTER_TYPE` can't be changed after creation. To change `EVENT_FILTER_TYPE`, create a new trigger and delete the old one.
   - Each trigger can have multiple event filters, comma delimited in one `--event-filters`=\[`ATTRIBUTE`=`VALUE`,...\] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported; however, when using the `--event-filters-path-pattern` flag, you can define a resource [path pattern](https://docs.cloud.google.com/eventarc/docs/path-patterns.md.txt).
   - Optionally, to specify a single event delivery attempt with no retries, use the `--max-retry-attempts` flag. The only valid value is `1`. If you omit the flag, the standard retry behavior applies. For more information, see [Retry events](https://docs.cloud.google.com/eventarc/docs/retry-events.md.txt).
   - Other flags are available. For more information, see [`gcloud eventarc triggers create`](https://docs.cloud.google.com/sdk/gcloud/reference/eventarc/triggers/create).

### Terraform

To create an Eventarc trigger for a Cloud Run service, see
[Create a trigger using Terraform](https://docs.cloud.google.com/eventarc/docs/creating-triggers-terraform.md.txt).

## Create a trigger for functions

After deploying a function, you can configure a trigger using the Google Cloud console,
Google Cloud CLI, or Terraform.

### Console

When you use the Google Cloud console to create a function, you can also add a
trigger to your function. Follow these steps to create a trigger for your
function:

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

   [Go to Cloud Run](https://console.cloud.google.com/run/services)
2. Click **Write a function** , and enter the function details. For more
   information about configuring functions during deployment, see
   [Deploy functions](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt#console).

3. In the **Trigger** section, click **Add trigger**.

4. Select **Cloud Storage trigger**.

5. In the **Eventarc trigger** pane, modify the
   trigger details as follows:

   1. Enter a name for the trigger in the **Trigger name** field, or use
      the default name.

   2. Select a **Trigger type** from the list:

      - **Google Sources** to specify triggers for
        Pub/Sub, Cloud Storage, Firestore,
        and other Google event providers.

      - **Third-party** to integrate with non-Google providers
        that offer an Eventarc source. For more information,
        see [Third-party events in Eventarc](https://docs.cloud.google.com/eventarc/docs/third-parties/third-parties-overview).

   3. Select **Cloud Storage** from the **Event provider** list, to select
      a product that provides the type of event for triggering your function.
      For the list of event providers, see
      [Event providers and destinations](https://docs.cloud.google.com/eventarc/docs/event-providers-targets.md.txt).

   4. Select **google.cloud.storage.object.v1.finalized** from the **Event type** list. Your
      trigger configuration varies depending on the supported event type.
      For more information, see [Event types](https://docs.cloud.google.com/eventarc/docs/event-types.md.txt).

   5. Leave the **Event data content type** field as is.

   6. In the **Bucket** field, click **Browse** to select a
      Cloud Storage bucket for the trigger to monitor. Changes to objects within this bucket will trigger calls to your function.

   7. If the **Region** field is enabled, select a
      [location](https://docs.cloud.google.com/eventarc/docs/overview.md.txt#trigger-location) for the
      Eventarc trigger. In general, the location of an
      Eventarc trigger should match the location of the
      Google Cloud resource that you want to monitor for events. In most
      scenarios, you should also deploy your function in the same region.
      See [Understand Eventarc locations](https://docs.cloud.google.com/eventarc/docs/understand-locations.md.txt)
      for more details about Eventarc trigger locations.

   8. In the **Service account** field, select a service account.
      Eventarc triggers are linked to service accounts to
      use as an identity when invoking your function. Your
      Eventarc trigger's service account must have the
      permission to invoke your function. By default, Cloud Run
      uses the [Compute Engine default service account](https://docs.cloud.google.com/compute/docs/access/service-accounts.md.txt#default_service_account).

   9. Optionally, specify the **Service URL path** to send the incoming
      request to. This is the relative path on the destination service to
      which the events for the trigger should be sent. For example:
      `/`, `/route`, `route`, and `route/subroute`.

   10. Optionally, to enable retries if the delivery attempt fails, select
       the **Enable retry on failure** checkbox; otherwise, the default
       behavior is a single delivery attempt with no retries. For more
       information, see [Retry events](https://docs.cloud.google.com/eventarc/docs/retry-events.md.txt).

6. Once you've completed the required fields, click **Save trigger**.

7. Click **Create**.

8. In the **Source** tab, edit the source code if needed, then select
   **Save and redeploy**.

### gcloud

When you create a function using the gcloud CLI, you must first
[deploy](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt) your function, and then create a
trigger. Follow these steps to create a trigger for your function:

1. Run the following command in the directory that contains the sample code
   to deploy your function:

       gcloud run deploy FUNCTION \
           --source . \
           --function FUNCTION_ENTRYPOINT \
           --base-image BASE_IMAGE_ID \
           --region REGION

   Replace the following:
   - `FUNCTION`: the name of the function you are
     deploying. You can omit this parameter entirely,
     but you will be prompted for the name if you omit it.

   - `FUNCTION_ENTRYPOINT`: the entry point to your function
     in your source code. This is the code Cloud Run executes when
     your function runs. The value of this flag must be a function name or
     fully-qualified class name that exists in your source code.

   - `BASE_IMAGE_ID`: the base image environment for your
     function. For more details about base images and the packages included
     in each image, see
     [Runtimes base images](https://docs.cloud.google.com/run/docs/configuring/services/runtime-base-images.md.txt#how_to_obtain_base_images).

   - `REGION`: the Google Cloud
     [region](https://docs.cloud.google.com/run/docs/locations.md.txt) where you want to deploy
     your function. For example, `europe-west1`.

2. Run the following command to create a trigger that filters and routes
   events:


       gcloud eventarc triggers create TRIGGER_NAME  \
           --location=LOCATION \
           --destination-run-service=FUNCTION  \
           --destination-run-region=DESTINATION_RUN_REGION \
           --event-filters="type=EVENT_FILTER_TYPE" \
           --event-filters="bucket=BUCKET" \
           --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

   <br />

   Replace the following:
   - `TRIGGER_NAME`: the ID of the trigger or a fully qualified identifier.
   - `LOCATION`: the location of the Eventarc trigger. Alternatively, you can set the `eventarc/location` property; for example, `gcloud config set eventarc/location us-central1`.

     To avoid any performance and data residency issues, the location must
     match the location of the Google Cloud service that is generating
     events. For more information, see
     [Eventarc locations](https://docs.cloud.google.com/eventarc/docs/locations.md.txt).
   - `FUNCTION`: the name of the deployed Cloud Run function that receives the events for the trigger.
   - `DESTINATION_RUN_REGION`: (optional) the [Cloud Run location](https://docs.cloud.google.com/run/docs/locations.md.txt) in which the destination Cloud Run function can be found. If not specified, it is assumed that the function is in the same region as the trigger.
   - `EVENT_FILTER_TYPE`: the identifier of the event. An event is generated when an API call for the method succeeds. For long-running operations, the event is only generated at the end of the operation, and only if the action is performed successfully. For a list of supported event types, see [Google event types supported by Eventarc](https://docs.cloud.google.com/eventarc/docs/reference/supported-events.md.txt#directly-from-a-google-cloud-source).
   - `BUCKET`: the globally unique identifier of the Cloud Storage bucket.
   - `SERVICE_ACCOUNT_NAME`: the name of your user-managed service account.
   - `PROJECT_ID`: your Google Cloud project ID.

   Notes:
   - The `--event-filters="type=EVENT_FILTER_TYPE"` flag is required. If no other event filter is set, events for all resources are matched.
   - `EVENT_FILTER_TYPE` can't be changed after creation. To change `EVENT_FILTER_TYPE`, create a new trigger and delete the old one.
   - Each trigger can have multiple event filters, comma delimited in one `--event-filters`=\[`ATTRIBUTE`=`VALUE`,...\] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported; however, when using the `--event-filters-path-pattern` flag, you can define a resource [path pattern](https://docs.cloud.google.com/eventarc/docs/path-patterns.md.txt).
   - Optionally, to specify a single event delivery attempt with no retries, use the `--max-retry-attempts` flag. The only valid value is `1`. If you omit the flag, the standard retry behavior applies. For more information, see [Retry events](https://docs.cloud.google.com/eventarc/docs/retry-events.md.txt).
   - Other flags are available. For more information, see [`gcloud eventarc triggers create`](https://docs.cloud.google.com/sdk/gcloud/reference/eventarc/triggers/create).

### Terraform

To create an Eventarc trigger for a Cloud Run function,
see [Create a trigger using Terraform](https://docs.cloud.google.com/eventarc/docs/creating-triggers-terraform.md.txt).

## Event delivery

Cloud Storage triggers are implemented with
[Pub/Sub notifications for Cloud Storage](https://docs.cloud.google.com/storage/docs/pubsub-notifications.md.txt).
Events are subject to
[Pub/Sub notification delivery guarantees](https://docs.cloud.google.com/storage/docs/pubsub-notifications.md.txt#delivery_guarantees).

A Cloud Storage bucket can have up to 10 notification configurations
set to trigger for a specific event. Exceeding the bucket's notifications limits
will cause further function deployments to fail with an error like the following:

```
Cloud Storage bucket ...: Pub/Sub notification limit reached
```

You can work around this limitation by
[managing your own Pub/Sub topic](https://docs.cloud.google.com/storage/docs/reporting-changes.md.txt)
and registering it for Cloud Storage notifications of the type you want your
function to respond to. For example, you could configure a single notification
for your chosen event (such as object creation) to a Pub/Sub topic.
Then, have as many function subscribers as needed to the topic process the event
and fan it out to multiple destinations. For example:

1. [Create your own Pub/Sub topic](https://docs.cloud.google.com/pubsub/docs/create-topic.md.txt).
2. Create a single [notification configuration](https://docs.cloud.google.com/storage/docs/reporting-changes.md.txt) on your Cloud Storage bucket that triggers on a specific event (such as `OBJECT_FINALIZE` for object creation) and sends the event to your single Pub/Sub topic.
3. Create a function (or functions) triggered by your [Pub/Sub topic](https://docs.cloud.google.com/run/docs/tutorials/pubsub-eventdriven.md.txt).

This results in one Cloud Storage notification configuration, one
Pub/Sub topic, and as many functions as you need (within the very high
[delivery limits](https://docs.cloud.google.com/pubsub/quotas.md.txt#resource_limits) allowed by Pub/Sub).

See Cloud Storage [Quotas and limits](https://docs.cloud.google.com/storage/quotas.md.txt) to learn more
about Cloud Storage limitations.

## Next steps

- Learn how to [trigger with events](https://docs.cloud.google.com/run/docs/triggering/trigger-with-events.md.txt).
- Learn how to [trigger services from Cloud Storage using Eventarc](https://docs.cloud.google.com/run/docs/tutorials/eventarc.md.txt).
- Learn how to [trigger functions from Cloud Storage using Eventarc](https://docs.cloud.google.com/run/docs/tutorials/trigger-functions-storage.md.txt).