如何从数据库触发的云端功能访问Firebase存储?
问题描述:
我想通过监听由functions.database.ref().onWrite()
(而不是functions.storage.object().onChange()
)触发的事件使用Firebase云功能执行文件操作。如何从数据库触发的云端功能访问Firebase存储?
我注意到使用云功能functions.storage.object().onChange()
触发云功能进行文件操作的大部分示例。有没有办法从functions.database.ref()
内获得storageRef
并从那里执行文件操作?
答
要从数据库中访问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 => {
...
+5
@FrankvanPuffeken我对使用Google Cloud SDK的部分有所了解,只是无法弄清楚如何从Cloud Function获取'bucket'。现在很明显'bucket'只是'bucket = functions.config()。firebase.storageBucket'。谢谢! –
另见https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for- firebase –