File Upload Next Js Supabase

8 min read Oct 06, 2024
File Upload Next Js Supabase

File Upload in Next.js with Supabase

File uploads are a fundamental part of many web applications, allowing users to share documents, images, and other data. Next.js, a popular React framework, simplifies the process of building dynamic web applications, including file uploads. In this article, we'll explore how to integrate file uploads into your Next.js application using Supabase, a powerful open-source database and storage service.

Why Supabase?

Supabase provides a seamless way to manage your application's data, including file storage. Its intuitive API and integration with Next.js make it a compelling choice for developers. Here's why Supabase is a great fit for file uploads in Next.js:

  • Ease of Integration: Supabase offers a simple and well-documented API for file uploads, making it easy to integrate into your Next.js project.
  • Scalable Storage: Supabase provides a reliable and scalable storage solution that can handle a wide range of file types and sizes.
  • Security: Supabase ensures the security of your data with robust authentication and access control mechanisms.
  • Cost-Effective: Supabase offers a generous free tier for smaller projects, making it a budget-friendly option.

Setting Up Supabase

Before we delve into the code, let's set up a Supabase project:

  1. Create a Supabase Account: Sign up for a free Supabase account at .
  2. Create a New Project: Create a new Supabase project to host your database and storage.
  3. Install Supabase CLI: Install the Supabase CLI tool to interact with your project from the command line:
    npm install -g supabase
    
  4. Initialize Supabase: Navigate to your project directory and initialize Supabase:
    supabase init
    
  5. Configure Supabase Environment Variables: Store your Supabase project credentials (URL, API Key) as environment variables in your Next.js project. This ensures secure storage of sensitive information.

Implementing File Upload in Next.js

Now, let's create a Next.js component that handles file uploads using Supabase:

import { useState } from 'react';
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

const supabase = createClient(supabaseUrl, supabaseAnonKey);

function FileUploadComponent() {
  const [selectedFile, setSelectedFile] = useState(null);
  const [uploadProgress, setUploadProgress] = useState(0);
  const [uploadError, setUploadError] = useState(null);

  const handleFileChange = (event) => {
    setSelectedFile(event.target.files[0]);
  };

  const handleFileUpload = async () => {
    if (!selectedFile) {
      setUploadError('Please select a file to upload.');
      return;
    }

    try {
      const { error, data } = await supabase.storage
        .from('uploads')
        .upload(selectedFile.name, selectedFile, {
          onProgress: (progress) => {
            setUploadProgress(progress.loaded / progress.total * 100);
          },
        });

      if (error) {
        setUploadError(error.message);
      } else {
        setUploadError(null);
        console.log('File uploaded:', data);
      }
    } catch (error) {
      setUploadError(error.message);
    }
  };

  return (
    

File Upload with Supabase

{uploadProgress > 0 && (
Upload Progress: {uploadProgress.toFixed(2)}%
)} {uploadError &&
{uploadError}
}
); } export default FileUploadComponent;

Explanation:

  1. Import Dependencies: Import necessary components from @supabase/supabase-js to interact with Supabase.
  2. Supabase Client: Create a Supabase client instance using your project's credentials (environment variables).
  3. State Management: Use useState to manage the selected file, upload progress, and potential errors.
  4. File Selection: The handleFileChange function updates the selectedFile state when a user selects a file.
  5. Upload Logic: The handleFileUpload function performs the upload:
    • Validation: Checks if a file is selected.
    • Upload: Uses supabase.storage.from('uploads').upload() to upload the file to the 'uploads' bucket in your Supabase storage.
    • Progress Tracking: The onProgress callback updates the uploadProgress state during the upload.
    • Error Handling: Handles potential errors and updates the uploadError state.
  6. UI Rendering: Renders the file input, upload button, progress indicator, and error messages based on the component state.

Accessing Uploaded Files

Once files are uploaded to Supabase, you can retrieve them using the Supabase storage API:

import { useState } from 'react';
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

const supabase = createClient(supabaseUrl, supabaseAnonKey);

function FileDisplayComponent() {
  const [files, setFiles] = useState([]);
  const [fetchError, setFetchError] = useState(null);

  useEffect(() => {
    const fetchFiles = async () => {
      try {
        const { data, error } = await supabase.storage
          .from('uploads')
          .list();

        if (error) {
          setFetchError(error.message);
        } else {
          setFiles(data);
        }
      } catch (error) {
        setFetchError(error.message);
      }
    };

    fetchFiles();
  }, []);

  return (
    

Uploaded Files

{fetchError &&
{fetchError}
} {files.map((file) => (

{file.name}

View File
))}
); } export default FileDisplayComponent;

Explanation:

  1. Fetch Files: The useEffect hook fetches a list of files from the 'uploads' bucket using supabase.storage.from('uploads').list().
  2. Error Handling: Handles potential errors while fetching files.
  3. Display Files: Renders a list of files, displaying their name and a link to view them in a new tab.

Conclusion

Integrating file uploads into your Next.js application with Supabase is a straightforward process. Supabase's robust storage features, easy integration, and scalability make it a great choice for managing your application's files. By following the steps outlined in this article, you can seamlessly implement file uploads, secure storage, and retrieval in your Next.js projects. Remember to configure your environment variables carefully to protect your Supabase credentials and leverage the benefits of Supabase for a smooth and secure file management experience.

Latest Posts