Get a Pub/Sub notification

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

Code sample


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 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 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}")

What's next

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