3 Hours

7. Storage: Files, Buckets & Access Control

Storage

S3-Compatible File Storage Backed by Postgres

Supabase Storage is an S3-compatible file storage service where access control is managed by Postgres RLS policies. Unlike raw S3, you don't need complex IAM rules — you write SQL policies, and Storage enforces them. Files are stored in a CDN with global edge caching for fast delivery.

Storage is built around the concept of buckets. A bucket is a container for files. Each bucket has a name and a public/private setting. Public buckets serve files without authentication. Private buckets require a signed URL or valid session token to access. The metadata for each file is stored in the storage.objects table in Postgres. This table includes the file path, size, MIME type, and owner. RLS policies on the storage.objects table control access to files. This architecture gives you the power of S3 with the flexibility of SQL-based access control. You can create sophisticated access policies based on file metadata, user attributes, or custom logic. This is a significant advantage over raw S3 where access control is limited to IAM roles and bucket policies.

Buckets: Public vs Private

Public buckets are ideal for avatars, product images, and public assets. Files have permanent, publicly accessible URLs. The CDN caches these files at the edge for fast delivery. Private buckets are for user documents, medical records, or any content that should be protected. Files in private buckets require a signed URL or an auth header to access. The signed URL is time-limited — you can set expiration times from seconds to days. When a user requests a file, the Storage API checks the RLS policies on the storage.objects table. If the user has permission, the file is served. This is secure and efficient. Choose public vs private based on your content type. Always default to private unless you have a specific need for public access.

Public Buckets

Files have publicly accessible URLs. No auth required to download. Use for avatars, product images, public assets. CDN serves files at the edge.

Private Buckets

Files require a signed URL or auth header to access. Use for user documents, medical records, private uploads. Access is validated per-request.

TypeScript
// Create a public bucket
const { data, error } = await supabase.storage.createBucket('avatars', {
  public: true,
  fileSizeLimit: '2MB',
});

Uploading Files

Uploading files is straightforward with the Supabase client. You upload a file to a specific path in a bucket. The path can be organized by user ID, date, or any structure you choose. The upload method accepts a File object, Blob, or Buffer. You can set cacheControl headers for CDN caching. The upsert option overwrites existing files. After uploading, you can get a public URL using getPublicUrl. For private buckets, use createSignedUrl to generate a time-limited URL. Always structure your file paths with the user's ID to make RLS policies easier. The convention is userId/filename.ext. This makes it easy to write policies that check the owner of a file. Store the file URL in your profiles table so you can easily reference it.

TypeScript
// Upload from a File input (browser)
const file = event.target.files[0];
const filePath = `${user.id}/${Date.now()}-${file.name}`;

const { data, error } = await supabase.storage
  .from('avatars')
  .upload(filePath, file, {
    cacheControl: '3600',
    upsert: true
  });

Storage RLS Policies

Storage policies live in the storage.objects table. The file path is available as name, and you can parse it to enforce user-specific access. The storage.foldername() function splits the path and returns the folder segments. This is useful for enforcing user-scoped storage. For example, you can allow users to upload only to their own folder by checking that the first segment of the path matches auth.uid(). For private buckets, you can restrict SELECT to the user's own files. For public buckets, you can allow SELECT for all users. These policies work exactly like table RLS policies. Use the same principles for writing storage policies as you do for table policies.

SQL
-- Allow users to upload only to their own folder
CREATE POLICY "user_upload_own_folder" ON storage.objects
  FOR INSERT
  WITH CHECK (
    bucket_id = 'avatars'
    AND auth.uid()::TEXT = (storage.foldername(name))[1]
  );
File Path Convention:Always prefix file paths with the user's ID: userId/filename.ext. The storage.foldername() function splits the path and returns the folder segments, making it easy to enforce user-scoped storage in policies.

Signed URLs for Private Files

Signed URLs provide temporary access to private files. You generate a signed URL with a specific expiration time. The URL includes a cryptographic signature that validates the request. Anyone with the URL can access the file, so treat signed URLs like temporary passwords. The default expiration is 60 seconds, but you can set it from a few seconds to several days. Use signed URLs when you need to share a private file with a specific person. For example, you can generate a signed URL for a user's invoice and send it via email. The user can download the invoice without needing to authenticate. This is secure because the URL expires after the specified time. Always set appropriate expiration times based on the sensitivity of the content.

TypeScript
// Generate a temporary signed URL (valid for 1 hour)
const { data } = await supabase.storage
  .from('documents')
  .createSignedUrl('user-id/contract.pdf', 3600);

Knowledge Check

Ready to test your understanding of 7. Storage: Files, Buckets & Access Control?

Back