Google 云存储更改通知 Node.js

Google Cloud Storage change notifications with Node.js

我有 Firebase 存储桶,我想使用 Node.js Google-云通知 API 来监听存储中的变化。

我目前拥有的:

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

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

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

现在据我了解,我必须创建一个频道才能收听存储更改。

所以我有:

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

这是文档不再清晰的阈值,因为我无法弄清楚 channelId a resourceId 代表什么。

我也不明白如何自己声明监听频道变化。是否有任何生命周期类型的方法可以这样做?

我可以做类似的事情吗?

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

基于现有的documentation of the Google Cloud Node.js Client and the feedback from this Github issue,节点客户端目前无法创建通道或订阅对象更改通知。

其中一个原因是使用客户端的机器不一定是运行应用程序的机器,因此存在安全风险。但是,仍然可以订阅给定存储桶的对象更改通知,并让通知收到 Node.js GAE 应用程序。

使用对象:watchAllJSONAPI

当使用 gsutil 订阅时,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,可以向所述 API 发送适当的 POST 请求并提供所需的端点 URL在 address。例如,address 可能是 https://my-node-app.example.com/change.

Node/Express 应用程序服务然后需要侦听 POST 对路径 /change 的请求,以便 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 云存储在失败时重试,或者在失败时停止重试成功了。