This project is a demonstration of how to upload files to Firebase Storage using Spring Boot.
- Java 21
- Maven
- Firebase account with access to Firebase Storage and a JSON credentials file
-
Configure Firebase
Make sure you have a project in Firebase and download the JSON credentials file. This file is used to authenticate the application with Firebase.
-
Set up environment variables
We use environment variables to configure access to Firebase. Create a
.env
file in the root directory of the project based on the example file.env.example
:FIREBASE_CREDENTIALS_PATH=/path/to/your/firebase/credentials.json FIREBASE_STORAGE_BUCKET_NAME=your-firebase-storage-bucket
Replace
/path/to/your/firebase/credentials.json
with the path to the JSON credentials file andyour-firebase-storage-bucket
with the name of your Firebase Storage bucket. -
Configure the
application.properties
fileThe
src/main/resources/application.properties
file should contain:spring.application.name=spring-firebase-upload firebase.credentials-path=${FIREBASE_CREDENTIALS_PATH} firebase.storage.bucket-name=${FIREBASE_STORAGE_BUCKET_NAME}
-
Load the environment variables
Make sure the environment variables are set. In IntelliJ IDEA, you can add the variables from the
.env
file to the environment variables in the run configurations:- Open the "Run/Debug Configurations" dialog.
- Select your run configuration.
- In the "Environment Variables" field, click the folder button.
- Choose the
.env
file in the file chooser. - Save the changes.
You can find the Jetbrains Official Tutorial here.
-
Run the application
mvn spring-boot:run
The application will be available at
http://localhost:8080
.
-
POST
/upload
This endpoint allows you to upload files to Firebase Storage. You can send files using the file parameter in the request.
Example usage with curl:
curl -F "file=@/path/to/your/file.txt" http://localhost:8080/upload
It will return the public link of the uploaded file.
Tests are set up to verify the upload, retrieval, and deletion of files in Firebase Storage. To run the tests, use the following command:
mvn test