Learn to set up a Supabase project, configure storage & bucket permissions, and generate a public URL for any Supabase file using a step-by-step guide.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Supabase Project
Before you can create a public URL for a Supabase storage file, ensure that you have a Supabase project set up. If you don’t have a project yet, go to the Supabase website and sign up or log in. Create a new project by following the on-screen instructions.
Step 2: Configure Supabase Storage
Go to the "Storage" section in your Supabase dashboard to configure storage for your project. Here, you can create a new "Bucket," which is a container for storing files.
Step 3: Set Bucket Permissions
To allow public access to a file, you need to configure the bucket permissions accordingly. By default, files in a Supabase bucket are private, so you need to change this setting.
Here’s an example SQL policy that grants read access to all users:
BEGIN;
CREATE POLICY "Public read access"
ON storage.objects
FOR SELECT USING (bucket_id = 'your_bucket\_name');
COMMIT;
Be sure to replace 'your_bucket_name'
with the actual name of your bucket.
Step 4: Upload Files to Supabase Storage
Upload your files to the configured bucket in Supabase storage.
Step 5: Obtain Public URL
Once the file is uploaded and the permissions are correctly set, you can generate a public URL.
For example, if your domain is 'xyz.supabase.co'
, your file's public path may look like this:
https://xyz.supabase.co/storage/v1/object/public/your_bucket_name/your_file_path
Step 6: Programmatic Access (Optional)
If you want to access the URL programmatically, you can use the Supabase client library.
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'https://xyz.supabase.co';
const supabaseKey = 'public-anon-key';
const supabase = createClient(supabaseUrl, supabaseKey);
const { data, error } = supabase
.storage
.from('your_bucket_name')
.getPublicUrl('your_file_path');
console.log(data.publicUrl);
Ensure 'your_bucket_name'
and 'your_file_path'
are replaced with your actual bucket name and file path.
By following these steps, you can create a public URL for any file stored within Supabase storage. Access to this URL will allow anyone on the internet to view the file provided they have the link.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.