A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.
Launch Xperto-AISupabase Storage is a powerful feature that allows developers to easily manage and store files in their applications. It provides a simple, scalable solution for handling user-generated content, application assets, and more. In this blog post, we'll dive deep into Supabase Storage and explore how to leverage its capabilities for efficient file management.
Before we start using Supabase Storage, we need to set it up in our project. Here's how:
import { createClient } from '@supabase/supabase-js' const supabase = createClient('YOUR_SUPABASE_URL', 'YOUR_SUPABASE_KEY') // Create a new bucket const { data, error } = await supabase .storage .createBucket('my-bucket-name', { public: false })
Uploading files to Supabase Storage is straightforward. You can use the upload
method to send files to your chosen bucket:
const avatarFile = event.target.files[0] const { data, error } = await supabase .storage .from('avatars') .upload('public/avatar1.png', avatarFile, { cacheControl: '3600', upsert: false })
In this example, we're uploading a file to the 'avatars' bucket with the name 'public/avatar1.png'. The upsert
option determines whether to overwrite an existing file with the same name.
To retrieve files from Supabase Storage, use the download
method:
const { data, error } = await supabase .storage .from('avatars') .download('public/avatar1.png') if (data) { const url = URL.createObjectURL(data) // Use the URL to display or process the file }
This code downloads the file 'public/avatar1.png' from the 'avatars' bucket and creates a URL for it.
Supabase Storage provides several methods for managing files:
To list files in a bucket:
const { data, error } = await supabase .storage .from('avatars') .list('public', { limit: 100, offset: 0, sortBy: { column: 'name', order: 'asc' }, })
To move a file within a bucket:
const { data, error } = await supabase .storage .from('avatars') .move('public/oldname.png', 'public/newname.png')
To delete a file:
const { data, error } = await supabase .storage .from('avatars') .remove(['public/avatar1.png'])
Supabase Storage allows you to control access to your files through bucket policies and Row Level Security (RLS):
When creating a bucket, you can set it as public or private:
const { data, error } = await supabase .storage .createBucket('my-public-bucket', { public: true })
Public buckets allow anyone to read the files, while private buckets require authentication.
For more granular control, you can use RLS policies:
CREATE POLICY "Allow users to upload avatars" ON storage.objects FOR INSERT TO authenticated WITH CHECK (bucket_id = 'avatars' AND auth.uid() = owner);
This policy allows authenticated users to upload files to the 'avatars' bucket, but only if they're the owner of the file.
Supabase Storage also offers advanced features like:
Here's an example of generating a signed URL:
const { data, error } = await supabase .storage .from('avatars') .createSignedUrl('public/avatar1.png', 60) if (data) { const signedUrl = data.signedUrl // Use the signed URL }
This creates a signed URL that's valid for 60 seconds, allowing temporary access to a private file.
By leveraging these features and following best practices, you can create a robust and efficient file management system for your applications using Supabase Storage.
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase
09/11/2024 | Supabase