Enterprise Storage, RLS & Image Transformations

Enterprise Storage, RLS & Image Transformations in Supabase: Supabase Storage provides scalable, S3-compatible object storage for user uploads, invoices, and…

Supabase Storage provides scalable, S3-compatible object storage for user uploads, invoices, and multimedia assets. Access permissions are governed directly by PostgreSQL RLS policies on the storage.objects table.


Bucket Configuration: Public vs Private

  • Public Buckets: Files have predictable public URLs accessible without tokens. Ideal for public avatars, blog post banners, and product catalog images.
  • Private Buckets: Files require authentication. Access is granted via signed URLs with temporary expiration or streaming through an authenticated API.
-- Create an enterprise private bucket for user documents
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
    'confidential_documents',
    'confidential_documents',
    false,
    20971520, -- 20 MB limit in bytes
    ARRAY['application/pdf', 'image/png', 'image/jpeg']
);

Storage Row Level Security Policies

The storage.objects table tracks all stored files. The name column contains the object path (e.g., user_123/invoices/inv_01.pdf):

-- Allow authenticated users to upload files only into their own folder
CREATE POLICY "Users upload to own folder"
ON storage.objects
FOR INSERT
TO authenticated
WITH CHECK (
    bucket_id = 'confidential_documents' AND
    (storage.foldername(name))[1] = (SELECT auth.uid()::text)
);

-- Allow users to view only their own files
CREATE POLICY "Users read own files"
ON storage.objects
FOR SELECT
TO authenticated
USING (
    bucket_id = 'confidential_documents' AND
    (storage.foldername(name))[1] = (SELECT auth.uid()::text)
);

File Uploads & Signed URL Generation

import { supabase } from '@/lib/supabase';

// 1. Uploading a file with path namespacing
async function uploadInvoice(file: File, userId: string) {
  const filePath = `${userId}/${Date.now()}_${file.name}`;

  const { data, error } = await supabase.storage
    .from('confidential_documents')
    .upload(filePath, file, {
      cacheControl: '3600',
      upsert: false // Prevent accidental overwriting
    });

  if (error) throw error;
  return data.path;
}

// 2. Generating a time-limited signed URL for secure viewing
async function getSecureDownloadUrl(filePath: string) {
  const { data, error } = await supabase.storage
    .from('confidential_documents')
    .createSignedUrl(filePath, 300); // URL expires in 300 seconds (5 min)

  if (error) throw error;
  return data.signedUrl;
}

Real-Time Image Transformations

Supabase includes an on-the-fly image optimization and transformation engine cached at the CDN edge:

// Transform image dimensions and format on the fly!
const { data } = supabase.storage
  .from('avatars')
  .getPublicUrl('profiles/user_402.png', {
    transform: {
      width: 250,
      height: 250,
      resize: 'cover',   // 'cover' | 'contain' | 'fill'
      format: 'webp',    // Automatically transcode to WebP for smaller payloads
      quality: 85
    }
  });

console.log('Optimized CDN URL:', data.publicUrl);

Exercises

  1. Create a storage RLS policy granting read access to members of an organization based on the organization ID in the path token.
  2. Implement a resumable upload handler using the TUS protocol for video files larger than 100 MB.
  3. Compare the security trade-offs between public buckets and short-lived signed URLs.