Skip to content

Storage

Files live in S3-compatible object storage, which can be a store you own: see Storage providers and your own S3. PotaLab Base checks each request against your policies and hands the client a short-lived signed URL, so file bytes go straight between the browser and the object store.

A bucket has rules: public, max_file_size (default 50 MiB) and allowed_mime_types (null allows any type, ["image/*"] allows a family). Create buckets in the dashboard (Storage → Buckets), with the Management API, or from a server with a secret key:

const base = createClient({ url, key: process.env.POTALAB_BASE_SECRET_KEY! })
await base.storage.createBucket("avatars", { public: false, maxFileSize: 5 * 1024 * 1024, allowedMimeTypes: ["image/*"] })
await base.storage.updateBucket("avatars", { public: true })
await base.storage.listBuckets()
await base.storage.getBucket("avatars")
await base.storage.emptyBucket("avatars")
await base.storage.deleteBucket("avatars") // 409 bucket_not_empty unless emptied

Public buckets are readable by anyone through an unsigned URL. Uploads are single requests up to the bucket’s size limit (also capped by your plan); multipart and resumable uploads are not available yet.

End users can do nothing until you add policies on storage.objects. A new project has none, so a signed-in user cannot upload anywhere by default. If your project already has the Owner preset on storage.objects (objects_all_owner), it lets every signed-in user write to any bucket under their own id: add bucket_id = '<bucket>' to it or drop it.

Operation Needs
upload INSERT (overwriting with upsert: true also needs UPDATE)
download, sign, list (private buckets) SELECT
delete SELECT and DELETE
-- users manage their own files in "avatars", under a folder named after their id
create policy avatars_insert on storage.objects for insert to authenticated
with check (bucket_id = 'avatars' and owner_id = (select auth.uid())
and (storage.foldername(path))[1] = (select auth.uid())::text);
create policy avatars_select on storage.objects for select to authenticated
using (bucket_id = 'avatars' and owner_id = (select auth.uid()));
create policy avatars_update on storage.objects for update to authenticated
using (bucket_id = 'avatars' and owner_id = (select auth.uid()));
create policy avatars_delete on storage.objects for delete to authenticated
using (bucket_id = 'avatars' and owner_id = (select auth.uid()));

Path helpers: storage.foldername(path) ('a/b/c.png' gives {a,b}), storage.filename(path) and storage.extension(path). owner_id is the uploader’s auth.uid(), NULL for anon and service uploads. Policies can only target the anon and authenticated roles, and other DDL on storage.objects and storage.buckets is refused.

const bucket = base.storage.from("avatars")
const { data, error } = await bucket.upload(`${userId}/avatar.png`, file, {
contentType: "image/png",
upsert: true,
})
await bucket.download(`${userId}/avatar.png`) // Blob, via a 5 minute signed URL
await bucket.createSignedUrl(`${userId}/avatar.png`, 60) // { signedUrl }; { download: true | "name.png" } for attachment
bucket.getPublicUrl("logo.png").data.publicUrl // public buckets only, no request, usable as <img src>
bucket.getPublicUrl("logo.png", { withApiKey: true }) // adds ?apikey=<publishable key> when the URL has no project host
await bucket.list(`${userId}/`, { limit: 100, offset: 0, search: "ava" })
await bucket.remove([`${userId}/avatar.png`])

The body can be a Blob, File, ArrayBuffer, typed array or string. upload() needs a known size, so ReadableStream bodies and progress events are not supported. Signed URLs default to 300 s and last at most 3600 s. An object a user may not see answers object_not_found, never “forbidden”, so existence is not leaked. remove skips paths the caller may not delete and takes at most 1000 paths per call.

Signed URLs, signed uploads, move and copy

Section titled “Signed URLs, signed uploads, move and copy”
import { createClient } from "@potalab/base"
const base = createClient({ url, key })
const bucket = base.storage.from("avatars")
// several read links at once; an entry you may not read has signedUrl: null and an error
const { data: links } = await bucket.createSignedUrls([`${userId}/a.png`, `${userId}/b.png`], 600)
// let someone without an account upload ONE file to ONE path
const { data: up } = await bucket.createSignedUploadUrl(`${userId}/inbox/scan.pdf`)
await bucket.uploadToSignedUrl(up.path, up.token, file, { contentType: "application/pdf" })
await bucket.move(`${userId}/a.png`, `${userId}/old/a.png`)
await bucket.copy(`${userId}/b.png`, `${userId}/b-copy.png`)

How signed links behave:

  • A read link is issued only if the caller may read the object (SELECT policy) and the object exists. It works without any key or session (usable as <img src>) until it expires (at most 3600 s). It is bound to that exact object: deleting or replacing the file, even at the same path, makes the old link answer 404.
  • A signed upload URL is checked against your INSERT policy (and UPDATE policy with { upsert: true }) when you create it. Whoever holds it can upload one file to exactly that path, once: a second upload answers 409 upload_token_used. It lasts 2 hours, or 5 minutes with upsert. If the uploaded file is deleted (or replaced) before the URL expires, the same URL can upload to that path again until it expires; your policies still apply. Bucket size and type rules and the project quota apply at upload time, and the uploader is recorded as the owner.
  • Links are signed for one canonical path. a//b.png, a/./b.png and ../ paths are refused instead of rewritten.
  • move needs the UPDATE policy on the object and the INSERT policy at the new path. copy needs SELECT on the source and counts as an upload of the copy (INSERT policy, quota).

The same endpoints answer @supabase/supabase-js storage calls (list, createSignedUrl(s), createSignedUploadUrl, uploadToSignedUrl, move, copy, remove). Differences from Supabase:

  • list sorts by name ascending only; any other sortBy is refused.
  • move and copy stay inside one bucket (destinationBucket is refused); copy has no upsert or copyMetadata options.
  • Send file bytes as an ArrayBuffer, typed array or Buffer: multipart bodies (supabase-js turns a Blob or File into one) are refused, including for uploadToSignedUrl.
  • Signed upload URLs are single-use, and last 5 minutes with upsert.
Code When
file_too_large (413), mime_type_not_allowed (415) bucket rules
quota_exceeded (413) project storage quota reached
forbidden (403) policies deny the operation, or bucket admin without a secret key
object_already_exists, upload_in_progress (409) path taken (use upsert: true), or another upload runs
upload_token_used (409) a signed upload URL was already used
object_not_found (404) missing or not visible to this user
upload_mismatch (422) stored object differs from the declared size or type
over_request_rate_limit (429) signed URL issuance rate limit