Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read and write to Firebase Storage from within Cloud Functions for Firebase?

I want to save a file from Cloud Functions for Firebase to Firebase Storage.

I also want to read the file from Firebase Storage to Cloud Functions for Firebase at a later time.

I couldn't find any example for the same. Can someone help?

like image 900
Sunny Tambi Avatar asked Mar 22 '17 19:03

Sunny Tambi


People also ask

How do you access Storage in Firebase?

From the navigation pane of the Firebase console, select Storage, then click Get started. Review the messaging about securing your Cloud Storage data using security rules. During development, consider setting up your rules for public access. Select a location for your default Cloud Storage bucket.


1 Answers

Cloud Functions run in a fairly standard V8 Node.js container. So you can use the regular Node SDK for Google Cloud Storage to interact with your files.

A snippet of code from the link:

// Upload a local file to a new file to be created in your bucket.
bucket.upload('/photos/zoo/zebra.jpg', function(err, file) {
  if (!err) {
    // "zebra.jpg" is now in your bucket.
  }
});

But really: just go to the link, it contains a better example than I'm willing to copy/paste here.

like image 91
Frank van Puffelen Avatar answered Oct 21 '22 03:10

Frank van Puffelen