10

I would like to perform file manipulation using firebase cloud functions by listening to an event triggered by functions.database.ref().onWrite() (instead of functions.storage.object().onChange()).

I noticed most of the sample using cloud functions functions.storage.object().onChange() to trigger a cloud functions for file manipulation. Is there a way to get a storageRef from within functions.database.ref() and perform file manipulation from there?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Andrew See
  • 1,062
  • 10
  • 21
  • Also see https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase – Doug Stevenson Jun 05 '17 at 21:15

1 Answers1

9

To access Firebase Storage from within your database triggered Cloud Function, you can use the Google Cloud SDK.

From one of my apps:

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 => {
      ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 8
    @FrankvanPuffeken I underderstand the part using Google Cloud SDK, just couldn't figure out how the get the `bucket` from Cloud Function. It is obvious to me now `bucket` is just `bucket = functions.config().firebase.storageBucket`. Thanks! – Andrew See Jun 07 '17 at 02:50
  • @FrankvanPuffeken I am using this way of getting a file, but I can't figure out how to fetch the file metadata from this object. Thanks in advance – Petr Jan 09 '19 at 17:34