Uploading Files to Firebase Cloud Storage in a Node.js Application

Awais Rai
2 min readJun 7, 2023

--

Uploading Files to Firebase Cloud Storage in a Node.js Application

Firebase offers a range of services that developers can take advantage of when creating web applications. One such service is Firebase Cloud Storage, which provides secure file uploads and downloads for your Firebase apps, regardless of network quality.

In this tutorial, we’ll walk through the process of uploading files to Firebase Cloud Storage using a Node.js application.

Requirements

  • Node.js and NPM installed
  • Firebase project
  • Firebase Admin SDK installed (npm install firebase-admin --save)
  • Service Account credentials (JSON file)

Setting Up the Firebase Admin SDK

First, let’s set up the Firebase Admin SDK in our Node.js application. The Admin SDK allows us to interact with Firebase from a server-side environment.

import * as admin from 'firebase-admin';

// Load your service account details
const serviceAccount = require('/path/to/your/serviceAccountKey.json');

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: '<BUCKET_NAME>.appspot.com', // Replace <BUCKET_NAME> with your bucket name
});

Replace '/path/to/your/serviceAccountKey.json' with the path to the JSON file containing your Service Account credentials. Remember to replace '<BUCKET_NAME>' it with the name of your Firebase Storage bucket.

Uploading Files to Firebase Cloud Storage

Now that the Admin SDK is set up, we can upload files to Firebase Cloud Storage. Here’s an example of how to do it:

import * as fs from 'fs';
import * as path from 'path';

const bucket = admin.storage().bucket();

const uploadFile = async (filePath: string) => {
const fileName = path.basename(filePath);
const file = bucket.file(fileName);

// Uploads a local file to the bucket
await bucket.upload(filePath, {
destination: file,
});

console.log(`${filePath} uploaded to ${file.name}.`);
};

// usage
const filePath = path.join(__dirname, './uploads/my-file.txt');
uploadFile(filePath).catch(console.error);

The uploadFile the function takes the path to the file you want to upload, creates a reference to a new file in the bucket with the same name, and uploads the file. The bucket.upload method uploads the file to the root of the Firebase Storage bucket.

If you want to upload the file to a specific directory in the bucket, you can modify the destination property in the bucket.upload method. If you want to upload files to a non-default bucket, you can pass the bucket name to the bucket() method like this: admin.storage().bucket('<BUCKET_NAME>').

Conclusion

Firebase Cloud Storage is a powerful service that allows for secure file uploads and downloads in your Firebase apps. With this tutorial, you should now be able to upload files to Firebase Cloud Storage in a Node.js application. Remember to handle errors that might occur during the file upload to make your application more robust. Happy coding!

--

--

Awais Rai
Awais Rai

Written by Awais Rai

Skilled Full-stack Developer experienced in React, Redux, NextJs, NodeJs, Express, Nest.js, AWS Lambda, RESTful & GraphQL API, and Docker.

No responses yet