如何从数据库触发的云功能中访问Firebase存储?

And*_*See 9 firebase google-cloud-functions firebase-storage

我想通过监听由functions.database.ref().onWrite()(而不是functions.storage.object().onChange())触发的事件来使用firebase云功能执行文件操作.

我注意到大多数使用云函数的示例functions.storage.object().onChange()触发了用于文件操作的云函数.有没有办法storageRef从内部获取functions.database.ref()并执行文件操作?

Fra*_*len 8

要从数据库触发的云功能中访问Firebase存储,您可以使用Google Cloud SDK.

来自我的一个应用:

const gcs = require('@google-cloud/storage')();

...
exports
.emojifyStory = functions.database.ref('/stories/{storyId}')
.onWrite(function(event) {
    const filePath = event.data.val().filePath;
    const file = gcs.bucket(bucket).file(filePath);

    // Use the Vision API to detect labels
    return visionClient.detectLabels(file)
      .then(data => {
      ...
Run Code Online (Sandbox Code Playgroud)

  • @FrankvanPuffeken我使用Google Cloud SDK来理解这部分,只是无法弄清楚如何从Cloud Function获取`bucket`.现在很明显`bucket`只是`bucket = functions.config().firebase.storageBucket`.谢谢! (7认同)