Laravel - How to set up Supabase as a file system?

2 min read 04-10-2024
Laravel - How to set up Supabase as a file system?


Leveraging Supabase as a File System in Your Laravel Application

Supabase, a powerful open-source Firebase alternative, offers an array of features including a robust database and user authentication. But did you know you can also use Supabase to store and manage your application's files? This article will guide you through the process of integrating Supabase as a file system in your Laravel application.

The Challenge

Imagine you're building a web application that requires users to upload images or documents. Traditionally, you'd use a dedicated file storage service like AWS S3 or Google Cloud Storage. However, using Supabase simplifies the process, offering a seamless integration with your existing database and authentication infrastructure.

Setting up Supabase

  1. Create a Supabase Project: If you don't already have one, head over to the Supabase website and create a new project. You'll get a unique project URL and API keys.

  2. Configure Your Laravel Project: Install the necessary packages in your Laravel application:

    composer require spatie/flysystem-supabase
    composer require league/flysystem-aws-s3-v3
    
  3. Create a Storage Disk: In your config/filesystems.php file, add a new disk configuration for Supabase:

    'disks' => [
        // ... existing disks
        'supabase' => [
            'driver' => 'supabase',
            'key' => env('SUPABASE_KEY'),
            'bucket' => env('SUPABASE_BUCKET'),
            'region' => env('SUPABASE_REGION'),
            'endpoint' => env('SUPABASE_ENDPOINT'),
        ],
    ],
    
    • Replace the placeholders with your actual Supabase API key, bucket name, region, and endpoint. You can find these values in your Supabase project dashboard.
  4. Set Up Environment Variables: Add the necessary environment variables to your .env file:

    SUPABASE_KEY=your_supabase_key
    SUPABASE_BUCKET=your_supabase_bucket
    SUPABASE_REGION=your_supabase_region
    SUPABASE_ENDPOINT=your_supabase_endpoint
    

Using Supabase for File Storage

Now you can leverage your new 'supabase' disk to store files in your Laravel application:

// Store a file
$file = $request->file('image');
$path = Storage::disk('supabase')->put('uploads', $file);

// Retrieve a file
$fileUrl = Storage::disk('supabase')->url('uploads/your-file.jpg');

// Delete a file
Storage::disk('supabase')->delete('uploads/your-file.jpg');

Advantages of Using Supabase

  • Simplified Integration: Supabase seamlessly integrates with your existing database and authentication infrastructure.
  • Cost-Effective: Supabase offers a generous free tier, making it a budget-friendly option for storing smaller file volumes.
  • Scalability: Supabase is built for scale, allowing you to handle large file uploads and downloads seamlessly.
  • Security: Supabase provides robust security measures to protect your data.

Conclusion

Using Supabase as a file system in Laravel offers a convenient and efficient solution for managing your application's file storage needs. This approach streamlines your development process, reduces costs, and ensures a secure environment for your data.

Additional Resources: