Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access Firebase storage from within a Database-triggered Cloud Function?

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?

like image 398
Andrew See Avatar asked Jun 05 '17 03:06

Andrew See


People also ask

How do I check my Firebase Storage?

To review your Cloud Storage billed usage, check the Usage and Billing dashboard. For resource usage, both the Cloud Storage Usage tab in the Firebase console and the metrics available through Cloud Monitoring can help you monitor Cloud Storage usage.

How do I use Firebase cloud function?

Implementation path. Install the Firebase CLI and initialize Cloud Functions in your Firebase project. Write JavaScript code (or TypeScript code to transpile at deployment) to handle events from Firebase services, Google Cloud services, or other event providers. Use the local emulator to test your functions.


1 Answers

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 => {
      ...
like image 62
Frank van Puffelen Avatar answered Sep 18 '22 00:09

Frank van Puffelen