What is Firebase Storage?
Firebase provides us with a storage mechanism, where we can create different buckets (in short rooms) to store a variety of items.
For example, one bucket can be used to store cat images, another bucket can be to store dog images. In short, you can store any kind of files onto Firebase storage.
To read on how one can upload a file to Firebase storage in Node.js , below is the link to the article.
How to upload files to Firebase Storage in Node.js
Firebase provides us with a storage mechanism, where we can create different buckets (in short rooms) to store…
Why do we need to use Firebase Storage?
Because Firebase Cloud Storage is a powerful yet easy-to-use tool for storing all sorts of objects. We can store images, audio, video, and other types of user content.
After a small introduction about Firebase Storage, let’s jump onto the main idea behind our article today, we will be looking into how one can integrate Node.js with Firebase storage and upload files onto the cloud.
What are the things we need to get started ?
- Gmail account.
- Familiarity with Node.js and how npm works.
Ok now without wasting any more time, let’s get onto the topic.
Install required Dependencies
Create a folder with any name and run the command
npm init . That will create a package.json file and few other config related files. Now, to install dependencies, run the below command inside the folder that you just created.
- npm i @google-cloud/storage
- npm i express
Steps to start a Firebase project
- Open the Firebase console.
- Click “CREATE NEW PROJECT”.
- The “Create a project” window opens.
- The start screen of the Firebase console opens.
- The “Enter app details” screen opens.
- Continue the configuration by following the on-screen instruction.
- Check the server key after the project is created.
- Down the server key and store it on your local drive for now.
Now that we have installed dependencies and created a Firebase project, just create an index.js file and paste the below code. Don’t worry, we will go over the code in detail, to understand what is happening.
Run the above code using the command
node index.js . After that navigate to your Storage tab where you can see all the files you have present in the bucket name provided by user.
Let us walkthrough the major points of the Code:-
- At the top we import all of our dependencies.
- After that on line 7 we import the firebase server key file that we created and downloaded in the above section.
- On line 11 we provide a bucket name that we want to create on Firebase cloud Storage.
- The downloadFile basically takes care of connecting to the Firebase cloud storage, connecting to the bucket with the provided name in the above step, if it doesn’t exist and then download the destFilename.
- We then call the method and run our server locally on the port-8088.
This summarizes our end of the Blog. Google Firebase team provides us with a very easy procedure, in order to connect and play around with storage.
Hope this article was helpful!