2017-06-05 9 views
5

の代わりにfunctions.database.ref().onWrite()によってトリガーされたイベントを聞いて、firebaseクラウド機能を使用してファイル操作を実行したいと考えています。データベーストリガークラウド機能内からFirebaseストレージにアクセスするにはどうすればよいですか?

ほとんどのサンプルがクラウド機能functions.storage.object().onChange()を使用して、ファイル操作用のクラウド機能をトリガーすることに気付きました。 storageReffunctions.database.ref()から取得し、そこからファイル操作を実行する方法はありますか?

+0

はまたhttps://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-を見ますfirebase –

答えて

6

データベースでトリガーされたクラウド機能の中からFirebase Storageにアクセスするには、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を使用してパーツを理解していますが、クラウド機能からどのようにバケットを得るのか分かりませんでした。 bucket = functions.config()。firebase.storageBucket'です。ありがとう! –