Resumable uploads to Supabase Storage
Supabase Storage accepts Tus uploads, so @uppy/tus can send files
to it directly from the browser. Each Tus request carries the signed-in user’s
Supabase access token, and Supabase applies your Storage policies to the upload.
Set up the bucket
Create a private bucket named uploads. In the Supabase SQL editor, add an
insert policy that lets users upload to a folder named after their user ID.
create policy "Users can upload to their own folder"
on storage.objects
for insert
to authenticated
with check (
bucket_id = 'uploads'
and (storage.foldername(name))[1] = (select auth.uid()::text)
);
Set the file size and MIME type restrictions your app needs on the bucket. Supabase also applies a global file size limit, which can’t exceed 50 MB on the Free plan.
The policy above only grants upload access. If users also need to get their files back, add read policies or use signed download links.
Configure Uppy
npm install @uppy/core @uppy/dashboard @uppy/tus @supabase/supabase-js
Call the function below in the browser once the user has signed in. Pass it
your app’s Supabase client, the project reference from your Supabase project
URL, and an existing HTML element to hold the uploader. Create the Supabase
client with your project’s publishable key (or the legacy anon key). Never
put a secret key or the legacy service_role key in browser code.
import Uppy from '@uppy/core'
import Dashboard from '@uppy/dashboard'
import Tus from '@uppy/tus'
import '@uppy/core/css/style.min.css'
import '@uppy/dashboard/css/style.min.css'
export async function setupUploader(supabase, projectRef, target) {
const { data, error } = await supabase.auth.getSession()
if (error) throw error
if (!data.session) throw new Error('Sign in to upload files')
const userId = data.session.user.id
const uppy = new Uppy({ id: `supabase-uploads-${userId}` })
.use(Dashboard, { inline: true, target })
.use(Tus, {
endpoint: `https://${projectRef}.storage.supabase.co/storage/v1/upload/resumable`,
chunkSize: 6 * 1024 * 1024,
uploadDataDuringCreation: true,
removeFingerprintOnSuccess: true,
allowedMetaFields: ['bucketName', 'objectName', 'contentType'],
async onBeforeRequest(request) {
const { data, error } = await supabase.auth.getSession()
if (error) throw error
if (data.session?.user.id !== userId) {
throw new Error('Sign in again to continue this upload')
}
request.setHeader('Authorization', `Bearer ${data.session.access_token}`)
},
})
uppy.on('file-added', (file) => {
uppy.setFileMeta(file.id, {
bucketName: 'uploads',
objectName: `${userId}/${crypto.randomUUID()}`,
contentType: file.type || 'application/octet-stream',
})
})
return uppy
}
The direct Storage hostname and the 6 MiB chunkSize come from Supabase’s
resumable uploads guide,
which says not to change the chunk size.
onBeforeRequest reads the session before every request, so later chunks pick
up a refreshed token. If the user signs out or switches accounts mid-upload,
Uppy retries the request a few times and then emits upload-error. That
error’s message describes the failed Tus request, and error.causingError
holds the error your onBeforeRequest threw.
The random UUID gives each new upload its own object path, so files with the
same name can coexist. Without it, Supabase rejects a second upload to the same
path with 400 Asset Already Exists. If the browser still holds an unfinished
upload of the same file, for example after a page reload, Uppy resumes it at
its original path and ignores the new objectName. A Supabase upload URL is
valid for up to 24 hours, after which the upload starts again from the
beginning.
Using the user ID in the Uppy instance ID keeps one account from picking up another account’s unfinished uploads in a shared browser.
Call uppy.destroy() when you remove the uploader from the page.
To test resuming, stay signed in and pick a file between 20 and 40 MiB that your bucket’s file size limit allows. Pause the upload in the Dashboard once more than 6 MiB has been sent, then resume it. Uppy asks Supabase how much it has already received and continues from there.