Get a Pub/Sub notification

Provides an example of how to get Pub/Sub notification metadata.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


For more information, see the Cloud Storage C# API reference documentation.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;

public class GetPubSubNotificationSample
    public Notification GetPubSubNotification(
        string bucketName = "your-unique-bucket-name",
        string notificationId = "notificationId")
        StorageClient storage = StorageClient.Create();
        Notification notification = storage.GetNotification(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;


For more information, see the Cloud Storage C++ API reference documentation.

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::runtime_error(notification.status().message());

  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";


For more information, see the Cloud Storage Go API reference documentation.

import (


// printPubsubBucketNotification gets a notification configuration for a bucket.
func printPubsubBucketNotification(w io.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: %v", err)
	defer client.Close()

	notifications, err := client.Bucket(bucketName).Notifications(ctx)
	if err != nil {
		return fmt.Errorf("Bucket.Notifications: %v", err)

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

	return nil


For more information, see the Cloud Storage Java API reference documentation.


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"

    Storage storage = StorageOptions.newBuilder().build().getService();
    Notification notification = storage.getNotification(bucketName, notificationId);
        "Found notification " + notification.getTopic() + " for bucket " + bucketName);


For more information, see the Cloud Storage Node.js API reference documentation.

 * 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('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function getMetadata() {
  // Get the notification metadata
  const [metadata] = await storage

  console.log(`ID: ${}`);
  console.log(`Topic: ${metadata.topic}`);
  console.log(`Event Types: ${metadata.event_types}`);
  console.log(`Custom Attributes: ${metadata.custom_attributes}`);
  console.log(`Payload Format: ${metadata.payload_format}`);
  console.log(`Object Name Prefix: ${metadata.object_name_prefix}`);
  console.log(`Etag: ${metadata.etag}`);
  console.log(`Self Link: ${metadata.selfLink}`);
  console.log(`Kind: ${metadata.kind}`);



For more information, see the Cloud Storage PHP API reference documentation.

use Google\Cloud\Storage\StorageClient;

 * Lists notification configurations for a bucket.
 * This sample is used on this page:
 * @param string $bucketName The name of your Cloud Storage bucket.
 * @param string $notificationId The ID of the notification.
function print_pubsub_bucket_notification(
    string $bucketName,
    string $notificationId
): void {
    // $bucketName = 'your-bucket';
    // $notificationId = 'your-notification-id';

    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $notification = $bucket->notification($notificationId);
    $notificationInfo = $notification->info();

Notification ID: %s
Topic Name: %s
Event Types: %s
Custom Attributes: %s
Payload Format: %s
Blob Name Prefix: %s
Etag: %s
Self Link: %s
        $notificationInfo['event_types'] ?? '',
        $notificationInfo['custom_attributes'] ?? '',
        $notificationInfo['blob_name_prefix'] ?? '',


For more information, see the Cloud Storage Python API reference documentation.

from import storage

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 = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    notification = bucket.get_notification(notification_id)

    print(f"Notification ID: {notification.notification_id}")
    print(f"Topic Name: {notification.topic_name}")
    print(f"Event Types: {notification.event_types}")
    print(f"Custom Attributes: {notification.custom_attributes}")
    print(f"Payload Format: {notification.payload_format}")
    print(f"Blob Name Prefix: {notification.blob_name_prefix}")
    print(f"Etag: {notification.etag}")
    print(f"Self Link: {notification.self_link}")


For more information, see the Cloud Storage Ruby API reference documentation.

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 =
  bucket  = storage.bucket bucket_name
  notification = bucket.notification notification_id

  puts "Notification ID: #{}"
  puts "Topic Name: #{notification.topic}"
  puts "Event Types: #{notification.event_types}"
  puts "Kind of Notification: #{notification.kind}"
  puts "Custom Attributes: #{notification.custom_attrs}"
  puts "Payload Format: #{notification.payload}"
  puts "Blob Name Prefix: #{notification.prefix}"
  puts "Self Link: #{notification.api_url}"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.