/supabase-tutorials

How to list files in Supabase storage bucket?

Learn how to list files from a Supabase storage bucket using JavaScript. Follow our step-by-step guide to set up your project, initialize the client, and handle errors.

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 list files in Supabase storage bucket?

 

Step 1: Set up Supabase Project

 

Before listing files from a Supabase storage bucket, ensure you have a Supabase project. If you don’t have a project, create one by visiting Supabase. Once the project is set up, obtain your project URL and the anon key from your Supabase dashboard, which are necessary for authentication.

 

Step 2: Install Dependencies

 

To interact with Supabase storage using JavaScript, you need to install the Supabase JavaScript client. If you're using Node.js, create a new project directory and initialize npm.

mkdir supabase-project
cd supabase-project
npm init -y
npm install @supabase/supabase-js

Alternatively, if you're working in a web environment, include the script in your HTML file:

<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>

 

Step 3: Initialize Supabase Client

 

In your JavaScript file, initialize the Supabase client using your Supabase project URL and anon key obtained from the Supabase dashboard.

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

// Replace 'your-supabase-url' and 'your-anon-key' with actual values
const supabaseUrl = 'your-supabase-url';
const supabaseAnonKey = 'your-anon-key';

const supabase = createClient(supabaseUrl, supabaseAnonKey);

 

Step 4: List Files in Supabase Storage Bucket

 

Once the client is initialized, you can list all files from a specific storage bucket. Replace 'your-bucket-name' with the actual name of your bucket.

async function listFiles() {
  try {
    const { data, error } = await supabase
      .storage
      .from('your-bucket-name')
      .list('');

    if (error) {
      console.error('Error listing files:', error.message);
      return;
    }

    console.log('Files in the bucket:');
    data.forEach(file => {
      console.log(`Name: ${file.name}, Size: ${file.size} bytes, Last Modified: ${file.updated_at}`);
    });

  } catch (error) {
    console.error('Error:', error);
  }
}

listFiles();

This function fetches information on all files in the specified bucket and logs their name, size, and last modified date.

 

Step 5: Execute Your Code

 

Run your JavaScript code to execute the function and list files in the storage bucket. If using Node.js, execute it in your terminal:

node your-js-file.js

If you're using a browser environment, ensure your script is linked in an HTML file, open the HTML file in a browser, and check the console for output.

 

Step 6: Handle Errors

 

Implement error handling to manage issues such as invalid bucket names or network errors. Always check if the error property is populated in the response and handle it accordingly in your code.

 

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