Methods

delete

delete(options, callback) returns Promise containing DeleteNotificationResponse

Permanently deletes a notification subscription.

Parameter

options

Optional

object

Configuration options.

Values in options have the following properties:

Parameter

userProject

Optional

string

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

callback

Optional

DeleteNotificationCallback

Callback function.

See also

Notifications: delete API Documentation

Returns

Promise containing DeleteNotificationResponse 

Example

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:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const notificationId = 'ID of notification to delete, e.g. 1';

// Deletes the notification from the bucket
await storage
  .bucket(bucketName)
  .notification(notificationId)
  .delete();

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

exists

exists(topic, options, callback) returns Promise containing CreateNotificationResponse

Creates a notification subscription for the bucket.

Parameter

topic

(Topic or 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

Optional

CreateNotificationRequest

Metadata to set for the notification.

callback

Optional

CreateNotificationCallback

Callback function.

See also

Notifications: insert

Throws

Error 

If a valid topic is not provided.

Returns

Promise containing CreateNotificationResponse 

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

exists(callback) returns Promise containing NotificationExistsResponse

Check if the notification exists.

Parameter

callback

Optional

NotificationExistsCallback

Callback function.

Returns

Promise containing 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

get(options, callback) returns Promise containing GetNotificationCallback

Get a notification and its metadata if it exists.

Parameter

options

Optional

object

Configuration options. See Bucket#createNotification for create options.

Values in options have the following properties:

Parameter

autoCreate

Optional

boolean

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

userProject

Optional

string

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

callback

Optional

GetNotificationCallback

Callback function.

See also

Notifications: get API Documentation

Returns

Promise containing GetNotificationCallback 

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

getMetadata(options, callback) returns Promise containing GetNotificationMetadataResponse

Get the notification's metadata.

Parameter

options

Optional

object

Configuration options.

Values in options have the following properties:

Parameter

userProject

Optional

string

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

callback

Optional

GetNotificationMetadataCallback

Callback function.

See also

Notifications: get API Documentation

Returns

Promise containing GetNotificationMetadataResponse 

Example

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:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const notificationId = 'ID of notification to get, e.g. 1';

// 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}`);