NextFire main logoNextFire
Docs X
Introduction
Get Started
Clone repo
Run the app
Configuration
Firebase
Create firebase app
Authentication
Firestore
Storage
Functions
Global configuration
Public
Site config
Payments
Stripe
One time
Subscriptions
Webhooks
Usage
Project structure
Static pages
Public pages
Protected pages
Auth
Client auth
Server auth
Data fetching
Firestore client
Firestore admin
Functions
Storage
Content
Blog
Documentation
Components
Markdown
Cards
Diagrams
Filetree
Lists
Maths
Notes
Steps
Table
Tabs
Public
Deep
Deeper
Even deeper
  1. Data Fetching
  2. Storage

Firebase Storage

How to upload, list, and delete files in Firebase Storage from the client in the boilerplate.

This boilerplate provides utilities to interact with Firebase Storage from the client side. You can upload files, list files under a folder, and delete files securely using a Firebase custom token for authentication.

1

Upload Image and Get URL

Use the uploadImageAndGetURL function to upload a file and retrieve its download URL:

import { uploadImageAndGetURL } from "@/lib/services/firebaseClient";

const handleUpload = async (file: File) => {
    try {
        const downloadUrl = await uploadImageAndGetURL(file, "images", user.customToken);
        console.log("Uploaded file URL:", downloadUrl);
    } catch (error) {
        console.error("Failed to upload image:", error);
    }
};

Notes:

  • The folder parameter is optional; defaults to "images".
  • A unique file name is generated using crypto.randomUUID().
  • Requires a valid Firebase custom token for authentication.
2

List Files Under a Folder

You can list all files in a storage folder and get their download URLs using listFilesUnderPath:

import { listFilesUnderPath } from "@/lib/services/firebaseClient";

const fetchFiles = async () => {
    try {
        const files = await listFilesUnderPath("images", user.customToken);
        console.log("Files in folder:", files);
    } catch (error) {
        console.error("Failed to list files:", error);
    }
};

Notes:

  • Returns an array of download URLs.
  • Requires authentication using a valid custom token.
3

Delete File

To delete a file from Firebase Storage, use deleteFileFromPath:

import { deleteFileFromPath } from "@/lib/services/firebaseClient";

const removeFile = async (fileUrl: string) => {
    try {
        await deleteFileFromPath(fileUrl, user.customToken);
        console.log("File deleted successfully");
    } catch (error) {
        console.error("Failed to delete file:", error);
    }
};

Notes:

  • Accepts either a full download URL or a relative storage path.
  • Extracts the storage path from the URL automatically if needed.
  • Requires authentication using a valid custom token.

This setup ensures secure file management from the client while keeping sensitive operations protected with Firebase authentication.

FunctionsContent

Content

FeedbackEdit page

© 2025 Ship IT.

Rubix Studios logo