How to Download files from Firebase Storage in Node.js

Kavit (zenwraight)
Jun 27, 2020 · 3 min read
Image for post
Image for post

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.

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 ?

  1. Gmail account.
  2. Basic knowledge of javascript.
  3. 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

  1. Open the Firebase console.
  3. The “Create a project” window opens.
  4. The start screen of the Firebase console opens.
  5. The “Enter app details” screen opens.
  6. Continue the configuration by following the on-screen instruction.
  7. Check the server key after the project is created.
  8. 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.

Image for post
Image for post

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!

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store