Uploading files to Firebase Storage using React
Tags: react, firebase, webdev
Published: 2022-05-08 13:00:49

Firebase Cloud Storage is designed to help quickly and easily store and serve user-generated content, such as photos and videos.
****

**By the end of this post you will learn how to :**

- Create and setup a firebase account
- Add your app to firebase
- Create Storage space in firebase
- Setup policies for storage access
- Upload photos using React Application

**What you should already know**

- Familiarity with writing React



**Firebase Setup**

We will setup the Firebase in three steps:
1. Firebase Account Creation
2. Creation of a new Project
3. Storage & Policy Setup
4. Coping configurations for connection

---

## - Firebase Account Creation:

   To create Firebase account go to [https://firebase.google.com/](https://firebase.google.com/), you can sign in using the Google Account.  Once logged in open the console using Go to console link or open [https://console.firebase.google.com/](https://console.firebase.google.com/)

## - Creation of a new Project

   You will have an option to add a project:
   ![Add A project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1g01l6bh2z6wa7ygmj3.png)
   Provide the required details to create a new project
   ![Project Name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jhzp55aortb2910rlfx.png)
   Set Google Analytics, for this demo we don't need this. 
   ![Google Analytics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c6wp4nljwx3q91z7ovb.png)
    Once you click on Create Project, in a few seconds  project will be ready to use.
   ![Project Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar22w0xpnzm2d1v1jrib.png)

## - Storage & Policy Setup

  Click on Storage
![Storage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tan731r681p24atihku5.png)
  Click on Get Started.
  Firebase will request you to select the security rules, if you are using this for development purposes you can select the Test mode which will allow you to access the data without authentication. For this demo, we will use Test mode. ( This is not recommended for production environments.)
![Policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0rj8c5lj6c3no1ylpdr.png)  

  Select the region where you want to store the files: 
![Region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kw9rmg5mw7mnr6uajcd7.png)  

  After a few seconds, your storage space will be ready. 
![Storage space](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j71k0z0y8zw1lwcn7zck.png)

## - Coping configurations for connection
   Firebase is all setup now we just have to copy the firebase config which will be required to connect to storage from react application. For this go to project overview and now we have to add an application to this project. 
![Web app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmdrcvlyzms3fo32rjrb.png)
   Click on Web App, choose a name and register app.
![Register](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyk5x8n4fxc9f2u4hi1r.png)
   You will be presented with the steps to use Firebase in Web Project. Copy it and save it for later use.
 ![Config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn4nh3f0tvjjqvg1qxg3.png)

---

## Create React App

Let's now start with building our React Application, you can refer https://reactjs.org/docs/create-a-new-react-app.html to create a basic structure of React App. 

- Install firebase dependency using below command:
`npm install firebase`
- Now, we will import the Firebase configs to the project.  Create a file src\config\firebase.js . Import the Firebase components in this file
`import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";`

- Copy the firebase config object we copied from firebase console and export storage constant
`export const app = initializeApp(firebaseConfig);
export const storage = getStorage(app);
`
This storage variable will allow us to upload and download the files in firebase storage. 

- In App component or any subcomponent create a Input type file and a button to upload file:
`<input type="file" onChange={onFileChange} />
<button onClick={onFileUpload}>Upload!</button>`

For writing the process for uploading the files to we need to import the storage variable and few components from firebase library.

`import { ref, uploadBytesResumable, getDownloadURL } from '@firebase/storage';
import { storage } from '../config/firebase';`

- On button's click even we will create a storage reference which will hold the storage and file location at firebase server.

`const storageRef = ref(storage, `/files/${file.name}`);`

![Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3e7acx1t6t2y9rdandnz.png)
 
- We will use `uploadBytesResumable`  to upload the file and getDownloadURL to get the URL from server by which we can download/view the uploaded file. `uploadBytesResumable` will return UploadTask which represents the process of uploading an object. It also allows you to monitor and manage the upload.

`const uploadTask = uploadBytesResumable(storageRef, file);`

  The event state_changed can be used to manage the upload and view the current state. 

- Now we are all done start the application and try uploading file.
![Demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1trp3e29db1z0lxgmis.gif)

![Firebse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdrz4w1rilp6zob2c4kg.png)

## Github:

You can refer the code here : https://github.com/ashusharmatech/react-firebase-storage-demo