[Overview](https://docs.cloud.google.com/storage/docs/pubsub-notifications)

This page describes how to configure your bucket to send notifications about
object changes to a Pub/Sub topic. For information on subscribing to a
Pub/Sub topic that receives notifications, see
[Choose a subscription type](https://docs.cloud.google.com/pubsub/docs/subscriber).

## Before you begin

Before using this feature, complete the following instructions.

### Enable the Pub/Sub API

Enable the Pub/Sub API for the project that will
receive notifications.

[Enable the API](https://console.cloud.google.com/apis/enableflow?apiid=pubsub)

### Make sure you have an existing Pub/Sub topic

If you haven't already, [create a Pub/Sub topic](https://docs.cloud.google.com/pubsub/docs/create-topic-console#create_a_topic) to which
you want to send notifications. This step is not necessary if you plan on using
the Google Cloud CLI or Terraform to perform the instructions on this page.

### Get required roles

IAM role requirements differ depending on whether you're
**configuring the notification** or **delivering the event data**:

- If you use the Google Cloud CLI or Terraform to configure a notification,
  your identity only needs permissions to update bucket metadata and view the
  Pub/Sub topic. To get all the permissions required, follow the
  instructions in
  [Get roles for viewing bucket metadata and the Pub/Sub topic](https://docs.cloud.google.com/storage/docs/reporting-changes#iam-bucket-metadata-pubsub-topic).

- If your Cloud Storage service agent is delivering notifications, it must
  have the Pub/Sub Publisher role (`roles/pubsub.publisher`) on the
  Pub/Sub topic. Once this role is configured for the service agent, the
  service agent acts as a background "worker" to push events to your topic.

  To grant your service agent the permissions required to push event
  notifications to your topic, follow the instructions in
  [Grant roles to your project's service agent](https://docs.cloud.google.com/storage/docs/reporting-changes#grant-required-role-to-service-agent).

#### Get roles for viewing bucket metadata and the Pub/Sub topic

To get the permissions that you need to configure and view Pub/Sub
notifications for a bucket, ask your administrator to grant you the following
roles. These predefined roles contain the permissions required to configure and
view Pub/Sub notifications.

- Storage Admin (`roles/storage.admin`) role on the bucket for which you want to
  configure Pub/Sub notifications

- Pub/Sub Admin (`roles/pubsub.admin`) role on the project in
  which you want to receive Pub/Sub notifications

You might be able to get these permissions with other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-permissions) or
[custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles).

See [Set and manage IAM policies on buckets](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions) for instructions on granting
roles on buckets. See [Controlling access](https://docs.cloud.google.com/pubsub/docs/access_control#console) for instructions on granting roles
on projects and setting access controls for topics and subscriptions.

#### Grant required role to your project's service agent

This section shows you how to grant the permissions required for your service
agent to deliver notifications.

1. [Get the email address of the service agent](https://docs.cloud.google.com/storage/docs/getting-service-agent) associated with
   the project that contains your Cloud Storage bucket. The service
   agent email address follows the format:

       service-PROJECT_NUMBER@gs-project-accounts.iam.gserviceaccount.com

2. Grant the service agent the Pub/Sub Publisher
   (`roles/pubsub.publisher`) role for the relevant Pub/Sub topic.
   See [Controlling access](https://docs.cloud.google.com/pubsub/docs/access_control#console) for instructions on granting roles for topics.

   > [!NOTE]
   > **Note:** There might be a delay of several seconds between assigning the role and having it applied to your service agent. If you grant this permission programmatically, wait 30 seconds before configuring Cloud Storage.

## Apply a notification configuration

The following steps add a [notification configuration](https://docs.cloud.google.com/storage/docs/pubsub-notifications#notification-configs) to your bucket that
sends notifications for all [supported events](https://docs.cloud.google.com/storage/docs/pubsub-notifications#events).

### Console

You cannot manage Pub/Sub notifications with the
Google Cloud console. Use the gcloud CLI or one of the available
client libraries instead.

### Command line

Use the [`gcloud storage buckets notifications create` command](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/notifications/create):

```
gcloud storage buckets notifications create gs://BUCKET_NAME --topic=TOPIC_NAME
```

Where:

- `BUCKET_NAME` is the name of the relevant
  bucket. For example, `my-bucket`.

- `TOPIC_NAME` is the Pub/Sub topic
  to send notifications to. If you specify a topic that doesn't exist
  in your project, the command creates one for you.

To send notifications for a subset of events, include the
[`--event-types` flag](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/notifications/create#--event-types).

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& topic_name) {
      StatusOr<gcs::NotificationMetadata> notification =
          client.CreateNotification(bucket_name, topic_name,
                                    gcs::NotificationMetadata());
      if (!notification) throw std::move(notification).status();

      std::cout << "Successfully created notification " << notification->id()
                << " for bucket " << bucket_name << "\n";
      std::cout << "Full details for the notification:\n"
                << *notification << "\n";
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class CreatePubSubNotificationSample
    {
        public Notification CreatePubSubNotification(
            string bucketName = "your-unique-bucket-name",
            string topic = "my-topic")
        {
            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            Notification notification = new Notification
            {
                Topic = topic,
                PayloadFormat = "JSON_API_V1"
            };

            Notification createdNotification = storage.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_CreateNotification_System_String_Google_Apis_Storage_v1_Data_Notification_Google_Cloud_Storage_V1_CreateNotificationOptions_(bucketName, notification);
            Console.WriteLine("Notification subscription created with ID: " + createdNotification.Id + " for bucket name " + bucketName);
            return createdNotification;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import (
    	"context"
    	"fmt"
    	"io"

    	"cloud.google.com/go/storage"
    )

    // createBucketNotification creates a notification configuration for a bucket.
    func createBucketNotification(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, projectID, bucketName, topic string) error {
    	// projectID := "my-project-id"
    	// bucketName := "bucket-name"
    	// topic := "topic-name"

    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	notification := storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Notification{
    		TopicID:        topic,
    		TopicProjectID: projectID,
    		PayloadFormat:  storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_NoPayload_JSONPayload,
    	}

    	createdNotification, err := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName).https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_AddNotification(ctx, &notification)
    	if err != nil {
    		return fmt.Errorf("Bucket.AddNotification: %w", err)
    	}
    	fmt.Fprintf(w, "Successfully created notification with ID %s for bucket %s.\n", createdNotification.ID, bucketName)
    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.EventType.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.PayloadFormat.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;
    import java.util.Map;

    public class CreateBucketPubSubNotification {

      public static void createBucketPubSubNotification(
          String bucketName,
          String topicName,
          Map<String, String> customAttributes,
          https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.EventType.html[] eventTypes,
          String objectNamePrefix,
          https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.PayloadFormat.html payloadFormat) {
        // The ID to give your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        // The name of the topic you would like to create a notification for
        // String topicName = "projects/{your-project}/topics/{your-topic}";

        // Any custom attributes
        // Map<String, String> customAttributes = Map.of("label", "value");

        // The object name prefix for which this notification configuration applies
        // String objectNamePrefix = "blob-";

        // Desired content of the Payload
        // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1;

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html notificationInfo =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html.newBuilder(topicName)
                .setCustomAttributes(customAttributes)
                .setEventTypes(eventTypes)
                .setObjectNamePrefix(objectNamePrefix)
                .setPayloadFormat(payloadFormat)
                .build();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html notification = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_createNotification_java_lang_String_com_google_cloud_storage_NotificationInfo_(bucketName, notificationInfo);
        String topic = notification.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html#com_google_cloud_storage_NotificationInfo_getTopic__();
        System.out.println("Successfully created notification for topic " + topic);
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // The name of a topic
    // const topic = 'my-topic';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function createNotification() {
      // Creates a notification
      await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html(topic);

      console.log('Notification subscription created.');
    }

    createNotification().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To create a notification configuration for a bucket using PHP, see the [Google Cloud Client Library](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.115.0/storage/notification?method=__construct) reference documentation.

<br />

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def create_bucket_notifications(bucket_name, topic_name):
        """Creates a notification configuration for a bucket."""
        # The ID of your GCS bucket
        # bucket_name = "your-bucket-name"
        # The name of a topic
        # topic_name = "your-topic-name"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()
        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)
        notification = bucket.notification(topic_name=topic_name)
        notification.create()

        print(f"Successfully created notification with ID {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_notification_id} for bucket {bucket_name}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    require "google/cloud/storage"

    def create_bucket_notifications bucket_name:, topic_name:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      # The ID of the pubsub topic
      # topic_name = "your-unique-topic-name"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name
      notification = bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html topic_name

      puts "Successfully created notification with ID #{notification.id} for bucket #{bucket_name}"
    end

<br />

### Terraform

You can use a [Terraform resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_notification) to add a notification
configuration to a bucket.

    // Create a Pub/Sub notification.
    resource "google_storage_notification" "notification" {
      provider       = google-beta
      bucket         = google_storage_bucket.bucket.name
      payload_format = "JSON_API_V1"
      topic          = google_pubsub_topic.topic.id
      depends_on     = [google_pubsub_topic_iam_binding.binding]
    }

    // Enable notifications by giving the correct IAM permission to the unique service account.
    data "google_storage_project_service_account" "gcs_account" {
      provider = google-beta
    }

    // Create a Pub/Sub topic.
    resource "google_pubsub_topic_iam_binding" "binding" {
      provider = google-beta
      topic    = google_pubsub_topic.topic.id
      role     = "roles/pubsub.publisher"
      members  = ["serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"]
    }

    resource "random_id" "bucket_prefix" {
      byte_length = 8
    }

    // Create a new storage bucket.
    resource "google_storage_bucket" "bucket" {
      name                        = "${random_id.bucket_prefix.hex}-example-bucket-name"
      provider                    = google-beta
      location                    = "US"
      uniform_bucket_level_access = true
    }

    resource "google_pubsub_topic" "topic" {
      name     = "your_topic_name"
      provider = google-beta
    }

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Create a JSON file that contains the following information:

   ```json
   {
     "topic": "projects/PROJECT_ID/topics/TOPIC_NAME",
     "payload_format": "JSON_API_V1"
   }
   ```

   Where:
   - `PROJECT_ID` is the ID for the project
     associated with the Pub/Sub topic that you want
     to send notifications to. For example, `my-pet-project`.

   - `TOPIC_NAME` is the Pub/Sub
     topic to send notifications to. For example, `my-topic`.

   To send notifications for a subset of events, include the
   [`event_types` field](https://docs.cloud.google.com/storage/docs/json_api/v1/notifications/insert#request-body) in the body of your JSON request.
3. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`POST notificationConfigs`](https://docs.cloud.google.com/storage/docs/json_api/v1/notifications/insert) request:

   ```
   curl -X POST --data-binary @JSON_FILE_NAME \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs"
   ```

   Where:
   - `JSON_FILE_NAME` is the path for the file
     that you created in Step 2.

   - `BUCKET_NAME` is the name of the bucket you
     want notifications to be generated for. For example, `my-bucket`.

### XML API

You cannot manage Pub/Sub notifications with the XML API.

### Apply a notification configuration across projects

Your bucket might be in a different project than the Pub/Sub topic you
want to send notifications to. For example, your bucket might be in Project A
while the Pub/Sub topic is in Project B. In this scenario, ensure the
following:

- In Project B, grant the Pub/Sub Publisher role
  (`roles/pubsub.publisher`) to Project A's Cloud Storage service agent on
  the target topic. For more details, see
  [Grant required permission to service agent](https://docs.cloud.google.com/storage/docs/reporting-changes#grant-required-role-to-service-agent).

- When creating the notification configuration in Project A, provide the full
  path to the topic in Project B. For example:

      projects/PROJECT_B_ID/topics/TOPIC_NAME

## Get a notification configuration

To get a specific notification configuration that's associated with your bucket,
complete the following steps:

### Console

You cannot manage Pub/Sub notifications with the
Google Cloud console. Use the Google Cloud CLI or one of the available
client libraries instead.

### Command line

Use the [`gcloud storage buckets notifications describe` command](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/notifications/describe):

```
gcloud storage buckets notifications describe projects/_/buckets/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID
```

Where:

- `BUCKET_NAME` is the name of the bucket whose
  notification configuration you want to retrieve For example,
  `my-bucket`.

- `NOTIFICATION_ID` is the ID number of the
  relevant configuration. For example, `5`.

If successful, the response looks similar to the following example:

```json
etag: '132'
id: '132'
kind: storage#notification
payload_format: JSON_API_V1
selfLink: https://www.googleapis.com/storage/v1/b/my-bucket/notificationConfigs/132
topic: //pubsub.googleapis.com/projects/my-project/topics/my-bucket
```

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& notification_id) {
      StatusOr<gcs::NotificationMetadata> notification =
          client.GetNotification(bucket_name, notification_id);
      if (!notification) throw std::move(notification).status();

      std::cout << "Notification " << notification->id() << " for bucket "
                << bucket_name << "\n";
      if (notification->object_name_prefix().empty()) {
        std::cout << "This notification is sent for all objects in the bucket\n";
      } else {
        std::cout << "This notification is sent only for objects starting with"
                  << " the prefix " << notification->object_name_prefix() << "\n";
      }
      std::cout << "Full details for the notification:\n"
                << *notification << "\n";
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class GetPubSubNotificationSample
    {
        public Notification GetPubSubNotification(
            string bucketName = "your-unique-bucket-name",
            string notificationId = "notificationId")
        {
            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            Notification notification = storage.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_GetNotification_System_String_System_String_Google_Cloud_Storage_V1_GetNotificationOptions_(bucketName, notificationId);

            Console.WriteLine("ID: " + notification.Id);
            Console.WriteLine("Topic: " + notification.Topic);
            Console.WriteLine("EventTypes: " + notification.EventTypes);
            Console.WriteLine("CustomAttributes: " + notification.CustomAttributes);
            Console.WriteLine("PayloadFormat: " + notification.PayloadFormat);
            Console.WriteLine("ObjectNamePrefix: " + notification.ObjectNamePrefix);
            Console.WriteLine("ETag: " + notification.ETag);
            Console.WriteLine("SelfLink: " + notification.SelfLink);
            Console.WriteLine("Kind: " + notification.Kind);

            return notification;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import (
    	"context"
    	"fmt"
    	"io"

    	"cloud.google.com/go/storage"
    )

    // printPubsubBucketNotification gets a notification configuration for a bucket.
    func printPubsubBucketNotification(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName, notificationID string) error {
    	// bucketName := "bucket-name"
    	// notificationID := "notification-id"

    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	notifications, err := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName).https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_Notifications(ctx)
    	if err != nil {
    		return fmt.Errorf("Bucket.Notifications: %w", err)
    	}

    	n := notifications[notificationID]
    	fmt.Fprintf(w, "Notification: %+v", n)

    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class PrintPubSubNotification {

      public static void printPubSubNotification(String bucketName, String notificationId) {
        // The ID to give your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        // The Pub/Sub topic you would like to find
        // String notificationId = "your-unique-notification-id"

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html notification = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_getNotification_java_lang_String_java_lang_String_(bucketName, notificationId);
        System.out.println(
            "Found notification " + notification.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html#com_google_cloud_storage_NotificationInfo_getTopic__() + " for bucket " + bucketName);
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // The ID of the notification
    // const notificationId = '1';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function getMetadata() {
      // Get the notification metadata
      const [metadata] = await storage
        .bucket(bucketName)
        .https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html(notificationId)
        .getMetadata();

      console.log(`ID: ${metadata.id}`);
      console.log(`Topic: ${metadata.https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/notificationmetadata.html}`);
      console.log(`Event Types: ${metadata.https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/notificationmetadata.html}`);
      console.log(`Custom Attributes: ${metadata.https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/notificationmetadata.html}`);
      console.log(`Payload Format: ${metadata.https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/notificationmetadata.html}`);
      console.log(`Object Name Prefix: ${metadata.https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/notificationmetadata.html}`);
      console.log(`Etag: ${metadata.etag}`);
      console.log(`Self Link: ${metadata.selfLink}`);
      console.log(`Kind: ${metadata.kind}`);
    }

    getMetadata().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To get a notification configuration for a bucket using PHP, see the [Google Cloud Client Library](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/storage/notification?method=info) reference documentation.

<br />

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def print_pubsub_bucket_notification(bucket_name, notification_id):
        """Gets a notification configuration for a bucket."""
        # The ID of your GCS bucket
        # bucket_name = "your-bucket-name"
        # The ID of the notification
        # notification_id = "your-notification-id"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()
        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)
        notification = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_get_notification(notification_id)

        print(f"Notification ID: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_notification_id}")
        print(f"Topic Name: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_topic_name}")
        print(f"Event Types: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_event_types}")
        print(f"Custom Attributes: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_custom_attributes}")
        print(f"Payload Format: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_payload_format}")
        print(f"Blob Name Prefix: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_blob_name_prefix}")
        print(f"Etag: {notification.etag}")
        print(f"Self Link: {notification.self_link}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    require "google/cloud/storage"

    def print_pubsub_bucket_notification bucket_name:, notification_id:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      # The ID of your notification configured for the bucket
      # notification_id = "your-notification-id"


      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name
      notification = bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html notification_id

      puts "Notification ID: #{notification.id}"
      puts "Topic Name: #{notification.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Notification.html}"
      puts "Event Types: #{notification.event_types}"
      puts "Kind of Notification: #{notification.kind}"
      puts "Custom Attributes: #{notification.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Notification.html}"
      puts "Payload Format: #{notification.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Notification.html}"
      puts "Blob Name Prefix: #{notification.prefix}"
      puts "Self Link: #{notification.api_url}"
    end

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`GET notificationConfigs`](https://docs.cloud.google.com/storage/docs/json_api/v1/notifications/get) request:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID"
   ```

   Where:
   - `BUCKET_NAME` is the name of the bucket
     whose notification configuration you want to retrieve. For
     example, `my-bucket`.

   - `NOTIFICATION_ID` is the ID number of the
     notification configuration you want to retrieve. For example, `5`.

### XML API

You cannot manage Pub/Sub Notifications with the XML API.

## List notification configurations for a bucket

To list all the notification configurations associated with a particular bucket:

### Console

You cannot manage Pub/Sub notifications with the
Google Cloud console. Use the gcloud CLI or one of the available
client libraries instead.

### Command line

Use the [`gcloud storage buckets notifications list` command](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/notifications/list):

```
gcloud storage buckets notifications list gs://BUCKET_NAME
```

Where `BUCKET_NAME` is the name of the bucket
whose notification configurations you want to list. For example,
`my-bucket`.

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name) {
      StatusOr<std::vector<gcs::NotificationMetadata>> items =
          client.ListNotifications(bucket_name);
      if (!items) throw std::move(items).status();

      std::cout << "Notifications for bucket=" << bucket_name << "\n";
      for (gcs::NotificationMetadata const& notification : *items) {
        std::cout << notification << "\n";
      }
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;
    using System.Collections.Generic;

    public class ListPubSubNotificationSample
    {
        public IReadOnlyList<Notification> ListPubSubNotification(string bucketName = "your-unique-bucket-name")
        {
            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            IReadOnlyList<Notification> notifications = storage.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_ListNotifications_System_String_Google_Cloud_Storage_V1_ListNotificationsOptions_(bucketName);

            foreach (Notification notification in notifications)
            {
                Console.WriteLine(notification.Id);
            }
            return notifications;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import (
    	"context"
    	"fmt"
    	"io"

    	"cloud.google.com/go/storage"
    )

    // listBucketNotifications lists notification configurations for a bucket.
    func listBucketNotifications(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName string) error {
    	// bucketName := "bucket-name"

    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	notifications, err := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName).https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_Notifications(ctx)
    	if err != nil {
    		return fmt.Errorf("Bucket.Notifications: %w", err)
    	}

    	for nID, n := range notifications {
    		fmt.Fprintf(w, "Notification topic %s with ID %s\n", n.TopicID, nID)
    	}

    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;
    import java.util.List;

    public class ListPubSubNotifications {

      public static void listPubSubNotifications(String bucketName) {
        // The ID to give your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        List<Notification> notificationList = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_listNotifications_java_lang_String_(bucketName);
        for (https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Notification.html notification : notificationList) {
          System.out.println(
              "Found notification " + notification.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.NotificationInfo.html#com_google_cloud_storage_NotificationInfo_getTopic__() + " for bucket " + bucketName);
        }
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function listNotifications() {
      // Lists notifications in the bucket
      const [notifications] = await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html();

      console.log('Notifications:');
      notifications.forEach(notification => {
        console.log(https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html.id);
      });
    }

    listNotifications().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To list notification configurations associated with a bucket using PHP, see the [Google Cloud Client Library](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.115.0/storage/bucket?method=notifications) reference documentation.

<br />

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def list_bucket_notifications(bucket_name):
        """Lists notification configurations for a bucket."""
        # The ID of your GCS bucket
        # bucket_name = "your-bucket-name"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()
        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)
        notifications = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_list_notifications()

        for notification in notifications:
            print(f"Notification ID: {notification.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_notification_id}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    require "google/cloud/storage"

    def list_bucket_notifications bucket_name:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name

      bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Policy-Bindings.html do |notification|
        puts "Notification ID: #{notification.id}"
      end
    end

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`GET notificationConfigs`](https://docs.cloud.google.com/storage/docs/json_api/v1/notifications/list) request:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs"
   ```

   Where `BUCKET_NAME` is the name of the bucket
   whose notification configurations you want to list. For
   example, `my-bucket`.

### XML API

You cannot manage Pub/Sub notifications with the XML API.

## Remove a notification configuration

To remove an existing notification configuration from your bucket:

### Console

You cannot manage Pub/Sub notifications with the
Google Cloud console. Use the gcloud CLI or one of the available
client libraries instead.

### Command line

Use the [`gcloud storage buckets notifications delete` command](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/notifications/delete):

```
gcloud storage buckets notifications delete projects/_/buckets/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID
```

Where:

- `BUCKET_NAME` is the name of the bucket whose
  notification configuration you want to delete. For example,
  `my-bucket`.

- `NOTIFICATION_ID` is the ID number of the
  configuration you want to delete. For example, `5`.

If successful, the response looks similar to the following example:

```json
Completed 1
```

Once sent, it might take up to 30 seconds for all notifications triggered
by the notification configuration to stop.

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& notification_id) {
      google::cloud::Status status =
          client.DeleteNotification(bucket_name, notification_id);
      if (!status.ok()) throw std::runtime_error(status.message());

      std::cout << "Successfully deleted notification " << notification_id
                << " on bucket " << bucket_name << "\n";
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using System;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;

    public class DeletePubSubNotificationSample
    {
        public void DeletePubSubNotification(
            string bucketName = "your-unique-bucket-name",
            string notificationId = "notificationId")
        {
            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            storage.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_DeleteNotification_System_String_System_String_Google_Cloud_Storage_V1_DeleteNotificationOptions_(bucketName, notificationId);

            Console.WriteLine("Successfully deleted notification with ID " + notificationId + " for bucket " + bucketName);
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import (
    	"context"
    	"fmt"
    	"io"

    	"cloud.google.com/go/storage"
    )

    // deleteBucketNotification deletes a notification configuration for a bucket.
    func deleteBucketNotification(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName, notificationID string) error {
    	// bucketName := "bucket-name"
    	// notificationID := "notification-id"

    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	bucket := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)

    	if err := bucket.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_DeleteNotification(ctx, notificationID); err != nil {
    		return fmt.Errorf("Bucket.DeleteNotification: %w", err)
    	}
    	fmt.Fprintf(w, "Successfully deleted notification with ID %s for bucket %s.\n", notificationID, bucketName)
    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class DeleteBucketPubSubNotification {

      public static void deleteBucketPubSubNotification(String bucketName, String notificationId) {
        // The ID to give your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        // The NotificationId for the notification you would like to delete
        // String notificationId = "your-unique-notification-id"

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        boolean success = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_deleteNotification_java_lang_String_java_lang_String_(bucketName, notificationId);
        if (success) {
          System.out.println("Successfully deleted notification");
        } else {
          System.out.println("Failed to find notification");
        }
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // The ID of the notification
    // const notificationId = '1';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function deleteNotification() {
      // Deletes the notification from the bucket
      await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html(notificationId).delete();

      console.log(`Notification ${notificationId} deleted.`);
    }

    deleteNotification().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To delete a notification configuration for a bucket using PHP, see the [Google Cloud Client Library](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.115.0/storage/notification?method=delete) reference documentation.

<br />

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def delete_bucket_notification(bucket_name, notification_id):
        """Deletes a notification configuration for a bucket."""
        # The ID of your GCS bucket
        # bucket_name = "your-bucket-name"
        # The ID of the notification
        # notification_id = "your-notification-id"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()
        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name)
        notification = bucket.notification(notification_id=notification_id)
        notification.delete()

        print(f"Successfully deleted notification with ID {https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.notification.BucketNotification.html#google_cloud_storage_notification_BucketNotification_notification_id} for bucket {bucket_name}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    require "google/cloud/storage"

    def delete_bucket_notification bucket_name:, notification_id:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      # The ID of your notification configured for the bucket
      # notification_id = "your-notification-id"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name
      notification = bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html notification_id
      notification.delete

      puts "Successfully deleted notification with ID #{notification_id} for bucket #{bucket_name}"
    end

<br />

### Terraform

To remove the notification configuration you created, run
[`terraform destroy`](https://www.terraform.io/cli/commands/destroy) from the folder containing your
Terraform file.

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`DELETE notificationConfigs`](https://docs.cloud.google.com/storage/docs/json_api/v1/notifications/delete) request:

   ```
   curl -X DELETE \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID"
   ```

   Where:
   - `BUCKET_NAME` is the name of the bucket
     whose notification configuration you want to delete. For
     example, `my-bucket`.

   - `NOTIFICATION_ID` is the ID number of the
     notification configuration you want to delete. For example, `5`.

Once sent, it may take up to 30 seconds for all notifications triggered
by the notification configuration to stop.

### XML API

You cannot manage Pub/Sub notifications with the XML API.

## What's next

- Learn more about
  [Pub/Sub notifications for Cloud Storage](https://docs.cloud.google.com/storage/docs/pubsub-notifications).

- [Create a subscription in Pub/Sub](https://docs.cloud.google.com/pubsub/docs/subscriber) to view notifications sent by
  Cloud Storage.

- See a [Python example app that polls for notifications on GitHub](https://github.com/googleapis/python-storage/blob/main/samples/snippets/notification_polling.py).

- Use Cloud Run functions to [deliver events with a Cloud Storage trigger](https://docs.cloud.google.com/functions/docs/tutorials/storage).