jankoritak
jankoritak

Reputation: 595

Google Cloud Storage change notifications with Node.js

I have Firebase storage bucket and I would like to use Node.js Google-cloud notification API in order to listen to changes in the storage.

What I have so far:

const gcloud = require('google-cloud');

const storage = gcloud.storage({
  projectId: 'projectId',
  credentials: serviceAccount
});  

const storageBucket = storage.bucket('bucketId');

Now from what I understand I have to create a channel in order to listen to storage changes.

So I have:

const storageBucketNotificationChannel = storage.channel('channelId', 'resourceId');

This is the threshold where the docs stop being clear, as I can't figure out what channelId a resourceId stand for.

Nor do I understand how to declare listening to channel changes itself. Are there any lifecycle-type methods to do so?

Can I do something like?

storageBucketNotificationChannel.onMessage(message => { ... })

Upvotes: 4

Views: 909

Answers (1)

Nicholas
Nicholas

Reputation: 1714

Based on the existing documentation of the Google Cloud Node.js Client and the feedback from this Github issue, there is presently no way for the node client to create a channel or subscribe to object change notifications.

One of the reasons being that the machine using the client may not necessarily be the machine on which the application runs, and thus a security risk. One can still however, subscribe to object change notifications for a given bucket and have notifications received a Node.js GAE application.

Using Objects: watchAll JSON API

When using gsutil to subscribe, gsutil sends a POST request to https://www.googleapis.com/storage/v1/b/bucket/o/watch where bucket is the name of the bucket to be watched. This is essentially a wrapper around the JSON API Objects: watchAll. Once a desired application/endpoint has been authorized as described in Notification Authorization, one can send the appropriate POST request to said API and provide the desired endpoint URL in address. For instance, address could be https://my-node-app.example.com/change.

The Node/Express application service would then need to listen to POST requests to path /change for notifications resembling this. The application would then act upon that data accordingly. Note, the application should respond to the request as described in Reliable Delivery for Cloud Storage to retry if it failed or stop retrying if it succeeded.

Upvotes: 2

Related Questions