Skip to content

File Uploads

Browser upload

Upload a file from an HTML file input:

const fileInput = document.getElementById("file-input") as HTMLInputElement
const file = fileInput.files?.[0]
if (file) {
const { data, error } = await supatype.storage
.from("avatars")
.upload(`user-${userId}/${file.name}`, file, {
contentType: file.type,
upsert: false,
})
if (error) {
console.error("Upload failed:", error.message)
} else {
console.log("Uploaded to:", data?.path)
}
}

Upload with React

function AvatarUpload({ userId }: { userId: string }) {
const [uploading, setUploading] = useState(false)
const handleUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
if (!file) return
setUploading(true)
const { data, error } = await supatype.storage
.from("avatars")
.upload(`${userId}/avatar.${file.name.split(".").pop()}`, file, {
upsert: true,
})
setUploading(false)
if (error) {
alert("Upload failed: " + error.message)
}
}
return (
<label>
{uploading ? "Uploading..." : "Choose avatar"}
<input
type="file"
accept="image/png,image/jpeg,image/webp"
onChange={handleUpload}
disabled={uploading}
hidden
/>
</label>
)
}

Upload from Node.js / server

upload takes a Blob, File or ArrayBuffer, so wrap a Node Buffer in a Blob:

import { readFile } from "fs/promises"
const fileBuffer = await readFile("./report.pdf")
const { data, error } = await supatype.storage
.from("documents")
.upload("reports/q1-2025.pdf", new Blob([fileBuffer]), {
contentType: "application/pdf",
})

Upload options

OptionTypeDefaultDescription
contentTypestringA File’s own type, else inferred by the storage service from the path’s extension (application/octet-stream when the extension is unknown)MIME type of the file
upsertbooleanfalseOverwrite if the file already exists. Without it, uploading to a path that is taken fails with 409

A Blob’s own type is not sent, so pass contentType for a Blob or an ArrayBuffer when the path’s extension does not identify the type.

Large files

Each upload is a single request: the storage service reads the whole body and stores it in one piece. There are no multipart or resumable uploads, so a dropped connection means uploading the file again. The size ceiling is the bucket’s fileSizeLimit (or maxSize in the schema) when it sets one, and otherwise the service-wide maximum (the storage service’s TIER_MAX_UPLOAD_SIZE, 50 MB by default); a larger file is refused with 413. An upload that would take the project’s stored total past the storage service’s quota (STORAGE_QUOTA, 1 GB by default, -1 for none) is refused with 507.

Organizing files

Use folder paths to organise files within a bucket:

avatars/
user-abc/avatar.png
user-def/avatar.png
documents/
user-abc/
reports/q1.pdf
reports/q2.pdf
user-def/
invoices/jan.pdf

Folders are only part of the path; access is not decided by them. Each object records the user who uploaded it in its owner column, and a bucket rule of BucketOwner compares the caller with that column, so “users may only delete their own files” is a rule on the bucket rather than a folder convention:

import type { Bucket, BucketLoggedIn, BucketOwner } from "@supatype/types"
export type Documents = Bucket<"documents", {
accessMode: "private"
access: { read: BucketOwner; create: BucketLoggedIn; delete: BucketOwner }
}>

One folder per user

Folder<N> is the Nth folder of the object’s path, counting from 1. Eq<Folder<1>, AuthUid> admits an object only when its first folder is the caller’s user id, the same rule Supabase writes as (storage.foldername(name))[1] = (select auth.jwt()->>'sub'):

import type { AuthUid, Bucket, Eq, Folder } from "@supatype/types"
type OwnFolder = Eq<Folder<1>, AuthUid>
export type Documents = Bucket<"documents", {
accessMode: "private"
access: { read: OwnFolder; create: OwnFolder; update: OwnFolder; delete: OwnFolder }
}>

User a can then upload a/report.pdf and not b/report.pdf, and a file at the root, which has no first folder, is refused. On update the rule is checked against the old name and the new one, so a rename cannot move a file into another user’s folder. Upload with the folder in the path: upload(`${user.id}/report.pdf`, file).

A bucket rule may use Public, Private, LoggedIn, BucketOwner, Role, AuthUid, AuthRole, Claim, Literal and Folder, joined with Any, All and Not. A rule that names a column of a model, or reads another table (In, Exists), cannot be evaluated on stored objects and is refused at push with the bucket and the operation named; write Private to refuse an operation on purpose.

A bucket with no rules of its own inherits its model’s access. A model rule that names one of its columns cannot be evaluated on stored objects, so on the bucket it denies everyone, and supatype diff and push warn about it naming the bucket, the model and the operation. Declare the rule in the bucket’s access instead.

Registered users only

Anonymous users pass LoggedIn. To keep guests out of a bucket, test the is_anonymous claim as well, as in anonymous sign-in:

import type { All, Bucket, Claim, Eq, Literal, LoggedIn } from "@supatype/types"
export type Uploads = Bucket<"uploads", {
accessMode: "private"
access: {
read: LoggedIn
create: All<[LoggedIn, Eq<Claim<"is_anonymous">, Literal<false>>]>
}
}>

File size limits

Set per-bucket file size limits when creating the bucket (a service role key is required):

await supatype.storage.createBucket("avatars", {
fileSizeLimit: 2 * 1024 * 1024, // 2 MB max
})

Allowed MIME types

Restrict which file types can be uploaded:

await supatype.storage.createBucket("images", {
allowedMimeTypes: ["image/png", "image/jpeg", "image/webp", "image/gif"],
})

Attempting to upload a file with a disallowed MIME type returns 415. Wildcards such as image/* are accepted. A bucket declared in the schema sets the same list with accept on its Bucket config.