/supabase-tutorials

How to create a public URL for Supabase storage file?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to create a public URL for Supabase storage file?

 

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.

  • Bucket Configuration: Ensure the bucket has the appropriate name fitting your project requirements.

 

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.

  • Permissions Setup: Enter the bucket's settings and navigate to the "Policies" section.
  • Policy Creation: Construct a policy to make the files within the bucket publicly accessible. Use the SQL editor provided by Supabase to write policies.

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.

  • File Upload: Use the file upload tool in your Supabase dashboard under the "Storage" section. Choose the target bucket and upload the desired files.

 

Step 5: Obtain Public URL

 

Once the file is uploaded and the permissions are correctly set, you can generate a public URL.

  • Fetch File URL: Navigate to the uploaded file in the Supabase dashboard and click on it.
  • Copy Public URL: The dashboard will display a URL for the file. Copy this URL— it’s the public link you will use to access the file from anywhere.

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.

  • Client Initialization: Initialize Supabase in your project with your URL and key.

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

const supabaseUrl = 'https://xyz.supabase.co';
const supabaseKey = 'public-anon-key';
const supabase = createClient(supabaseUrl, supabaseKey);
  • Fetch File URL Programmatically: Retrieve the public URL using the API.

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.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022