Cloud Storage

Cloud Storage for Firebase is built for app developers who need to store and serve user-generated content, such as photos or videos.

1. Initialize Storage

import { getStorage } from "firebase/storage";
const storage = getStorage(app);

2. Upload a File

First, create a Reference to the path where you want the file to live.

import { ref, uploadBytes } from "firebase/storage";

const storageRef = ref(storage, 'images/profile.jpg');

// 'file' comes from an <input type="file">
uploadBytes(storageRef, file).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

3. Get Download URL

After uploading, you usually need a URL to display the image in your app.

import { getDownloadURL } from "firebase/storage";

getDownloadURL(storageRef)
  .then((url) => {
    // You can use this URL as src for an <img> tag
    console.log("Download URL:", url);
  });

4. Delete a File

import { deleteObject } from "firebase/storage";

deleteObject(storageRef).then(() => {
  console.log("File deleted successfully");
});
Pro Tip: Cloud Storage scales automatically to petabytes of data, so you don't have to worry about server disk space!