2017-06-05 82 views
5

我想通過監聽由functions.database.ref().onWrite()(而不是functions.storage.object().onChange())觸發的事件使用Firebase雲功能執行文件操作。如何從數據庫觸發的雲端功能訪問Firebase存儲?

我注意到使用雲功能functions.storage.object().onChange()觸發雲功能進行文件操作的大部分示例。有沒有辦法從functions.database.ref()內獲得storageRef並從那裏執行文件操作?

+0

另見https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for- firebase –

回答

6

要從數據庫中訪問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'。謝謝! –

相關問題