Notification

Notification

A Notification object is created from your Bucket object using Bucket#notification. Use it to interact with Cloud Pub/Sub notifications.

Constructor

new Notification(bucket, id)

Parameters:
Name Type Description
bucket Bucket

The bucket instance this notification is attached to.

id string

The ID of the notification.

See:
Example
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const notification = myBucket.notification('1');

Members

metadata

The API-formatted resource description of the notification.

Note: This is not guaranteed to be up-to-date when accessed. To get the latest record, call the getMetadata() method.

Methods

delete(optionsopt, callbackopt) → {Promise.<DeleteNotificationResponse>}

Permanently deletes a notification subscription.

Parameters:
Name Type Attributes Description
options object <optional>

Configuration options.

Properties
Name Type Attributes Description
userProject string <optional>

The ID of the project which will be billed for the request.

callback DeleteNotificationCallback <optional>

Callback function.

Returns:
Type Description
Promise.<DeleteNotificationResponse>
See:
Examples
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const notification = myBucket.notification('1');

notification.delete(function(err, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
notification.delete().then(function(data) {
  const apiResponse = data[0];
});

Another example:

  /**
   * 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 deleteNotification() {
    // Deletes the notification from the bucket
    await storage.bucket(bucketName).notification(notificationId).delete();

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

  deleteNotification().catch(console.error);

exists(topic, optionsopt, callbackopt) → {Promise.<CreateNotificationResponse>}

Creates a notification subscription for the bucket.

Parameters:
Name Type Attributes Description
topic Topic | string

The Cloud PubSub topic to which this subscription publishes. If the project ID is omitted, the current project ID will be used.

Acceptable formats are:

  • projects/grape-spaceship-123/topics/my-topic

  • my-topic

options CreateNotificationRequest <optional>

Metadata to set for the notification.

callback CreateNotificationCallback <optional>

Callback function.

Returns:
Type Description
Promise.<CreateNotificationResponse>
See:
Throws:

If a valid topic is not provided.

Type
Error
Example
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const notification = myBucket.notification('1');

notification.create(function(err, notification, apiResponse) {
  if (!err) {
    // The notification was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
notification.create().then(function(data) {
  const notification = data[0];
  const apiResponse = data[1];
});

exists(callbackopt) → {Promise.<NotificationExistsResponse>}

Check if the notification exists.

Parameters:
Name Type Attributes Description
callback NotificationExistsCallback <optional>

Callback function.

Returns:
Type Description
Promise.<NotificationExistsResponse>
Example
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const notification = myBucket.notification('1');

notification.exists(function(err, exists) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
notification.exists().then(function(data) {
  const exists = data[0];
});

get(optionsopt, callbackopt) → {Promise.<GetNotificationCallback>}

Get a notification and its metadata if it exists.

Parameters:
Name Type Attributes Description
options object <optional>

Configuration options. See Bucket#createNotification for create options.

Properties
Name Type Attributes Description
autoCreate boolean <optional>

Automatically create the object if it does not exist. Default: false.

userProject string <optional>

The ID of the project which will be billed for the request.

callback GetNotificationCallback <optional>

Callback function.

Returns:
Type Description
Promise.<GetNotificationCallback>
See:
Example
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const notification = myBucket.notification('1');

notification.get(function(err, notification, apiResponse) {
  // `notification.metadata` has been populated.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
notification.get().then(function(data) {
  const notification = data[0];
  const apiResponse = data[1];
});

getMetadata(optionsopt, callbackopt) → {Promise.<GetNotificationMetadataResponse>}

Get the notification's metadata.

Parameters:
Name Type Attributes Description
options object <optional>

Configuration options.

Properties
Name Type Attributes Description
userProject string <optional>

The ID of the project which will be billed for the request.

callback GetNotificationMetadataCallback <optional>

Callback function.

Returns:
Type Description
Promise.<GetNotificationMetadataResponse>
See:
Examples
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const notification = myBucket.notification('1');

notification.getMetadata(function(err, metadata, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
notification.getMetadata().then(function(data) {
  const metadata = data[0];
  const apiResponse = data[1];
});

Another example:

  /**
   * 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
      .bucket(bucketName)
      .notification(notificationId)
      .getMetadata();

    console.log(`ID: ${metadata.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}`);
  }

  getMetadata().catch(console.error);