Your supabase storage upload worked in preview, then production returned 403 AccessDenied, new row violates row-level security policy, or a bucket error that makes no sense.
The file picker works. The progress bar moves. Sometimes the exact same code works when you test it as the founder and fails for a real user. The AI-generated app is not randomly broken. Storage is enforcing rules that were never finished.
supabase storage is not just a folder in the cloud
A bucket looks like a folder in the dashboard, but every object is also a row in storage.objects. Supabase uses row level security on that table to decide who may upload, download, list, move, copy, update, delete, and create signed URLs.
That is the part AI builders often skip. They create a bucket, upload one test file through the dashboard or use a privileged key during development. Production then uses the browser client with the public anon key and a real user session, and the missing policy becomes visible.
A public bucket adds another confusion. Public does not mean “anyone may upload.” It only means a file in that bucket can be downloaded through its public URL. Uploading, listing and other file operations still require policies. Use the normal storage path for those operations, not the public download URL.
What you see when storage fails
403 AccessDenied: the operation is not allowed by the storage policy.new row violates row-level security policy: the insert or upsert does not pass the policy check.404 NoSuchBucket: the bucket name is wrong, the bucket does not exist, or the client cannot access it.401 InvalidJWT: the user session is missing, expired or malformed.409 KeyAlreadyExists: the path is already taken and upsert is off.400 InvalidMimeType: the bucket or project limit rejects the file type.413 EntityTooLarge: the file is larger than the configured size limit.
The exact code matters. A policy fix will not repair a file-size rejection. Renaming a bucket will not repair an expired login token. Open DevTools, reproduce the upload once, and read the failed network response before changing code.
Why the AI built it this way
AI coding tools optimize for visible progress. They wire the upload control, pick a bucket name, store the returned path, and render the image or download link. If the preview test used the dashboard, a service key, or a user with broad permissions, the happy path looks complete.
The dangerous shortcut is the service role key. It bypasses RLS and can perform unrestricted storage operations. That is acceptable only inside a trusted backend. In browser code it turns every visitor into a storage administrator. If the AI put that key in frontend code, rotate it and remove it before doing anything else.
The correct model is narrower: the browser uses the public anon key, the user is signed in, and policies decide exactly which paths that user may write or read. The supabase anon key can stay public because the policy layer is the real lock.
Step 1: capture the real failed request
- Open a private browser window and sign in as a normal test user.
- Open DevTools → Network before uploading.
- Retry the upload once.
- Open the failed request to the storage endpoint.
- Copy the HTTP status, error code, message, bucket name and object path.
- Check whether the request used the anon key plus user JWT or a service key.
Do not prompt the AI with “storage does not work.” Paste the exact error and path into the fix request.
Step 2: verify bucket and path separately
Bucket names are exact. Avatars, avatars and avatar are different buckets. Confirm the bucket exists in the correct Supabase project, then compare it with the string in code.
The object path is separate from the bucket. In this call, avatars is the bucket and user-id/avatar.png is the object name:
supabase.storage
.from('avatars')
.upload('user-id/avatar.png', file, { upsert: false })
Policies often use the first folder as the owner. If the policy expects the user id as the first folder, an upload to the bucket root will fail even though the bucket exists. Align the code path and the policy instead of loosening the rule for everyone.
Step 3: add the narrow upload policy
A simple authenticated upload policy can restrict writes to one bucket and to a folder named after the user id:
create policy "users upload to their own folder"
on storage.objects
for insert
to authenticated
with check (
bucket_id = 'avatars'
and (storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
This allows an authenticated user to upload only when the first path segment is their own id. It does not allow them to list or overwrite the whole bucket. If your app stores company files rather than user files, use the company or tenant id in the path and policy instead.
For downloads in a private bucket, add a separate narrow SELECT policy. Keep read and write rules separate; one broad rule for everything is how private files leak.
Step 4: handle upsert as a different operation
Plain upload needs an INSERT policy. Overwriting with upsert: true is not the same permission shape. Supabase documents that upsert additionally needs SELECT and UPDATE permissions. The confusing 403 can appear even when insert looks correct because the database must read and return object metadata during the overwrite flow.
If you do not need overwrite behavior, omit upsert or set it to false. A duplicate path then returns a clear “already exists” style error, which your app can handle by choosing a new file name.
If overwrite is a real feature, keep upsert: true and add narrow SELECT and UPDATE policies for the same user-owned paths. Do not solve it by granting every authenticated user access to every object.
Step 5: decide public or private deliberately
Use a public bucket for assets that are meant to be public, such as product images or public avatars. Treat the URL as public forever. Do not store identity documents, invoices, exports, medical files or user-private uploads there.
Use a private bucket for user files. Downloads then need either a policy-backed authenticated request or a signed URL. Signed URLs skip the policy check when they are used because Supabase already checked permission when the URL was created. Creating the signed URL still needs the correct permission.
Step 6: check file limits and MIME types
If the status is 413 or InvalidMimeType, stop editing policies. Compare the real file size and type with the bucket or project storage settings. Large media uploads may need a higher limit, compression before upload, or resumable upload logic. A rejected MIME type needs either a narrower client file input or a deliberate bucket configuration change.
supabase storage production checklist
- Correct Supabase project and exact bucket name
- Upload tested as a normal signed-in user, not only as the founder
- Browser uses the anon key, never the service role key
INSERTpolicy onstorage.objectsmatches bucket and path- Private downloads have a narrow
SELECTpolicy or signed-URL flow upsert: truealso has narrowSELECTandUPDATEpolicies- Public buckets contain only files meant to be public
- File size and MIME limits match real uploads
- RLS on normal tables is handled separately — see supabase rls
- The launch checklist in lovable deploy is complete
Storage is finished only when a real user can upload, read back exactly what they own, and cannot touch anyone else’s files with the public key.
FAQ
Why do supabase storage uploads fail in production?
Most production failures come from missing or mismatched RLS policies on storage.objects, a wrong bucket name or path, an expired user JWT, file size or MIME limits, or upsert requiring SELECT and UPDATE permissions in addition to INSERT.
Does a public bucket mean anyone can upload files?
No. A public bucket only means files can be downloaded through a public URL. Upload, list, move, copy, update, delete and signed URL creation still need the correct storage policies.
Should I use the service role key to fix supabase storage errors?
Only on a trusted server. The service role bypasses RLS and gives unrestricted storage access. Never ship it in browser code, a mobile app or a public repository. Fix the policy instead.