Supabase is a powerful backend-as-a-service that provides authentication, database, and storage solutions. If you’re working with Laravel and want to connect Supabase Storage using the S3 driver, this guide will walk you through the process.
Why Use Supabase Storage with Laravel?
- Scalability: Supabase Storage uses S3-compatible APIs, making it easier to scale file storage.
- Security: Supabase provides authentication and access controls.
- Laravel Integration: Laravel supports the S3 driver, allowing seamless integration.
Step 1: Create a Supabase Project
- Go to Supabase and sign in.
- Create a new project and note down the Project URL and Anon/Public Key.
- Enable Storage by navigating to Storage > Buckets, and create a new bucket.
Step 2: Get Supabase Storage S3-Compatible Credentials
Supabase Storage uses an S3-compatible API. To retrieve your credentials:
- Navigate to Storage > Settings.
- Create S3 Credentials for storage.
- Copy the S3 Endpoint, Access Key, and Secret Key.
Step 3: Configure Laravel to Use Supabase Storage
Laravel provides built-in support for S3 storage. To configure it:
1. Install AWS S3 Package
Run the following command:
composer require league/flysystem-aws-s3-v3
2. Set Up Environment Variables
Add the following to your .env file:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=your_supabase_access_key
AWS_SECRET_ACCESS_KEY=your_supabase_secret_key
AWS_DEFAULT_REGION=us-east-1 # Supabase uses us-east-1 by default
AWS_BUCKET=bucket_name
AWS_ENDPOINT=https://{project-name}.supabase.co/storage/v1/s3/bucket_name
AWS_USE_PATH_STYLE_ENDPOINT=true
note: make sure to add bucket_name at the end of AWS_ENDPOINT.
3. Update Laravel Filesystem Configuration
Modify config/filesystems.php to include the Supabase storage setup:
disks' => [
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', true),
],
],
Step 4: Upload and Retrieve Files from Supabase Storage
Now, you can use Laravel’s Storage facade to interact with Supabase Storage.
Upload a File
use Illuminate\Support\Facades\Storage;
$file = request()->file('image');
$path = Storage::disk('s3')->put('uploads', $file);
return response()->json(['path' => $path]);
Retrieve a File URL
$url = Storage::disk('s3')->url('uploads/example.jpg');
return response()->json(['url' => $url]);
Step 5: Test Your Setup
Run the following command to verify file uploads:
php artisan tinker
Then, execute:
Storage::disk('s3')->put('test.txt', 'Hello, Supabase!');
If no errors occur, your Laravel app is successfully connected to Supabase Storage via S3.
Pros and Cons
- Pro: Easy integration with Laravel's filesystem.
- Con: Your files will be stored under "storage/v1/s3/bucket_name/bucket_name/" by default, which may require custom handling for direct file paths.
Conclusion
By leveraging Supabase’s S3-compatible storage with Laravel, you can manage file uploads efficiently without relying on traditional cloud providers. This setup provides security, scalability, and seamless integration with Laravel’s filesystem features.