This guide explains how to configure Firebase as a storage solution for the CDN File Uploader. By following these steps, you'll be able to store your uploaded files on Firebase Storage.
- Firebase Account: You need a Firebase account. If you don't have one, you can sign up at https://firebase.google.com/.
- Firebase Project: A Firebase project should be set up in the Firebase Console.
- Go to the Firebase Console.
- Click on Add Project and follow the steps to create a new Firebase project.
- Once the project is created, you will be directed to the Firebase project dashboard.
-
In the Firebase Console, go to the Storage section.
-
Click on Get Started to enable Firebase Storage for your project.
-
Set your storage rules according to your requirements. For example, to allow unauthenticated uploads, you can set the rules to:
service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if true; } } }
Note: For production, it is recommended to secure your storage rules properly. You can update them to restrict access based on authentication or other criteria.
- In the Firebase Console, go to Project Settings.
- Select the Service Accounts tab.
- Click Generate New Private Key. This will download a JSON file containing your Firebase credentials.
- Rename this file to
firebase.json
and place it in the root of your project.
Update your .env
file with the necessary Firebase environment variables. Make sure you add the following:
STORAGE=firebase
FIREBASE_STORAGE_BUCKET=gs://your-firebase-bucket
PUBLIC_URL_FIREBASE=https://firebasestorage.googleapis.com/v0/b/your-firebase-bucket/o
- Replace
gs://your-firebase-bucket
with the bucket name from the Firebase Storage console. It should look something likegs://your-project-id.appspot.com
. PUBLIC_URL_FIREBASE
is the base URL for accessing your files via Firebase Storage. This URL will be used to build the public URL for accessing uploaded files.
You need to install the Firebase Admin SDK to enable Firebase functionality in your Node.js app.
Run the following command:
npm install firebase-admin
To initialize Firebase in your project, create a file named config/firebase.js
where you will set up Firebase using the credentials and bucket you configured.
- Create a new file in
config/
namedfirebase.js
:
// config/firebase.js
const admin = require("firebase-admin");
const fireCreds = require("../firebase.json");
let bucket = null;
// Cek apakah aplikasi Firebase sudah diinisialisasi
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(fireCreds),
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
});
}
bucket = admin.storage().bucket();
module.exports = bucket;
-
This file will initialize Firebase if the
STORAGE=firebase
variable is set in the.env
file and export thebucket
object for further use in file upload logic. -
Import the Firebase bucket in your route or controller where you handle file uploads:
// Example: controllers/fileController.js
const bucket = require("../config/firebase.js");
if (process.env.STORAGE === 'firebase') {
// Your logic for uploading files to Firebase
}
Once Firebase is set up, the application will automatically store files in Firebase Storage if STORAGE=firebase
is set in the .env
file. When a file is uploaded, it will be uploaded to Firebase Storage and will be accessible via a URL generated by Firebase.
For example, you can access the uploaded file at:
https://firebasestorage.googleapis.com/v0/b/your-firebase-bucket/o/your-file-name?alt=media
This URL can also be built dynamically in the app using the PUBLIC_URL_FIREBASE
variable.
If you want to deploy your project to Vercel, make sure to add the necessary environment variables in the Vercel dashboard under the Environment Variables section.
The required environment variables are:
STORAGE=firebase
FIREBASE_STORAGE_BUCKET
PUBLIC_URL_FIREBASE
- You can also upload the
firebase.json
key file to Vercel securely.
After following the steps outlined above, your application will be configured to store uploaded files in Firebase Storage. Make sure to test the functionality locally before deploying the project to production.
For further assistance, you can refer to the official Firebase Storage documentation.