0
Challenge · 72Services

Storage: buckets, RLS on files & signed URLs

Storageused in ~75% of apps

DevLog needs avatar uploads. The instinct is to treat Storage as a separate product with its own rules — special dashboard, special permissions screen. It isn't. `storage.objects` is a Postgres table, and "a user may only touch their own avatar" is a policy you already know how to write, on a table you've never seen, with one new trick: the folder in a file path is just a column, and the row you're securing is a file.

Core idea. Storage buckets are namespaces backed by two ordinary tables — `storage.buckets` and `storage.objects` — and `storage.objects` takes RLS with the exact USING/WITH CHECK grammar from concept 53. The "userId-as-first-folder" convention (store every file at `<uid>/name.ext`) turns four policies — one per command — into a single repeated pattern: `(storage.foldername(name))[1] = auth.uid()::text`. A public bucket skips RLS for reads only — writes are always gated; a private bucket needs either a passing SELECT policy or a time-boxed signed URL; and a signed *upload* URL minted with the service role bypasses RLS entirely, so the server that mints it must validate the path itself.
Playground, this isn't a screenshot
as
touching
storage.foldername(name)
[alice-uid][1] =auth.uid()::textforbob-uid= false
Denied — bob does not own alice-uid. USING fails: list()/download return nothing, no error.

"Public" only ever changes the select row above. Every insert, update and deletepolicy still runs, public bucket or not — "public" means anyone can read the URL, never that anyone can write to it.

↑ play a little first, then dig in