How to upload images or files on Firebase Cloud Storage with Nextjs 14
In this tutorial, we will learn how to upload images or files on Firebase Cloud Storage with Nextjs 14.
Video Tutorial
You can watch the video tutorial for a better understanding.
{% youtube hrlmbRo1iOQ %}
What is Firebase Cloud Storage?
Firebase is a Backend-as-a-Service (BaaS) that provides a number of services like authentication, real-time database, cloud functions, and cloud storage is one of them. In cloud storage, you will be able to store media files like images, videos, pdfs and so on.
Firebase storage works similary to local file system. You can create directories and store files in them. You can also nest directories inside other directories.
Like local system, you also have path to the file in the cloud storage. I.E images/2022/01/01/my-image.png
, videos/2022/01/01/my-video.mp4
, etc.
I will use Nextjs but you can use any other frontend framework like React, Angular, Vue, etc.
Setup
I would recommend watching the video tutorial for a better understanding.
Create a new project on Firebase.
Create a new web app and copy the firebase configuration. Store that in a file
config.js
. Values should be on environment variables. Create a new storage reference usinggetStorage
and export that.1import { initializeApp } from 'firebase/app'2import { getStorage } from 'firebase/storage'34// Your web app's Firebase configuration5const firebaseConfig = {6 apiKey: 'AIzaSyCFZ6457rjJOFKLpd7b2HDSsc9hs1CUlVE',7 authDomain: 'firestorage-yt.firebaseapp.com',8 projectId: 'firestorage-yt',9 storageBucket: 'firestorage-yt.appspot.com',10 messagingSenderId: '372871883935',11 appId: '1:372871883935:web:bd07a58b0ff9c5ea5586aa',12}1314// Initialize Firebase15const app = initializeApp(firebaseConfig)1617const storage = getStorage(app)1819export { storage }Install the firebase package on Nextjs project.
Create storage from the console and use the test mode
Upload Images or files
1import { useRef } from 'react'2import { ref, uploadBytes } from 'firebase/storage'3import { storage } from './config'45const Uploader = () => {6 const inputRef = useRef(null)78 const handleUpload = async e => {9 e.preventDefault()1011 const file = inputRef.current.files[0]1213 try {14 const fileRef = ref(storage, `images/${file.name}`)15 await uploadBytes(fileRef, file)16 } catch (error) {17 console.log(error)18 }19 }2021 return (22 <div>23 <form onSubmit={handleUpload}>24 <input ref={inputRef} type='file' name='file' />25 <button type='submit'>Upload</button>26 </form>27 </div>28 )29}
Explanation:
- Created a new form with an input field of type 'file'. Without the input field, we can't select the file from the local system.
- Pass a
ref
to the input field so that we can access the file. - We get the file from
inputRef.current.files[0]
. The return should be an File object. - Create a reference to the file in the cloud storage using
ref
. - Use
uploadBytes
to upload the file to the cloud storage. There are many different format you can use for uploading. Like File, Blob, Uint8Array, etc. Check the docs
This is how you can upload images or files on Firebase Cloud Storage with Nextjs 14. To learn how to display upload progress, state or how to pause and resume the upload, check the video tutorial.
Shameless Plug
I also have firebase firestore series on my youtube channel. If you are interested, you can check that out.
{% youtube PoTUX9_3LaQ %}
If the blog has been helpful, please consider subscribe to my channel and drop a 💖. It will help me a lot.
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: