As part of the capstone project I am working on towards my bachelor degree, I need to have the Nodejs server I set up on Raspberry PI to receive incoming requests from other embedded systems such as NodeMCU or ESP8266 and upload images to Firebase Storage. If you have heard or used Firebase before, you probably know that Firebase provides API for communicating with their Realtime Database, Storage, Analytics, and many other awesome features that can power up your application.
However, Firebase Storage API doesn’t work on Nodejs. After spending lots of time in researching, I realize since Firebase was acquired by Google, many technology Firebase is using is actually what Google had before. In other words, Firebase Storage is utilizing Google Cloud Storage and Google has a tool for you if you would like to use it on your server.
First thing you need to do is to obtain the service account JSON from your Firebase project console. You should be able to obtain one from project settings. Next you need the project ID of your Firebase project. You can also check your project id from the project settings.
So now everything should be ready and since I am using Nodejs for the server and for this example, it’s time to create a folder and initialize a package.json:
You will need to install these modules for the following sample code:
npm install --save @google-cloud/storage express firebase multer
Or if you are using yarn:
yarn add @google-cloud/storage express firebase multer
The module multer is a middleware that handles form data for you so you can interact with the file that client uploads within the request.
The following code sample is a snippet from the original code I have for my capstone project. I have turned the process of uploading image file to cloud storage into a function that returns a promise to increase its reusability.
You can view the code gist from (here)[https://gist.github.com/stardustxx/58748550699228174b805aaadfc98a35].
The original article that I referred to was linked below:
If you like what you read or you find it useful, please feel free to share it with anybody that might need this! I’m still learning but I’m happy to share if what I’ve learned can help you!