Resumable Uploads
Learn how to upload files to Supabase Storage.
The resumable upload method is recommended when:
- Uploading large files that may exceed 6MB in size
- Network stability is a concern
- You want to have progress events for your uploads
Supabase Storage implements the TUS protocol to enable resumable uploads. TUS stands for The Upload Server and is an open protocol for supporting resumable uploads. The protocol allows the upload process to be resumed from where it left off in case of interruptions. This method can be implemented using the tus-js-client library, or other client-side libraries like Uppy that support the TUS protocol.
For optimal performance when uploading large files you should always use the direct storage hostname. This provides several performance enhancements that will greatly improve performance when uploading large files.
Instead of https://project-id.supabase.co use https://project-id.storage.supabase.co
Here's an example of how to upload a file using tus-js-client:
1const tus = require('tus-js-client')23const projectId = ''45async function uploadFile(bucketName, fileName, file) {6 const { data: { session } } = await supabase.auth.getSession()78 return new Promise((resolve, reject) => {9 var upload = new tus.Upload(file, {10 // Supabase TUS endpoint (with direct storage hostname)11 endpoint: `https://${projectId}.storage.supabase.co/storage/v1/upload/resumable`,12 retryDelays: [0, 3000, 5000, 10000, 20000],13 headers: {14 authorization: `Bearer ${session.access_token}`,15 'x-upsert': 'true', // optionally set upsert to true to overwrite existing files16 },17 uploadDataDuringCreation: true,18 removeFingerprintOnSuccess: true, // Important if you want to allow re-uploading the same file https://github.com/tus/tus-js-client/blob/main/docs/api.md#removefingerprintonsuccess19 metadata: {20 bucketName: bucketName,21 objectName: fileName,22 contentType: 'image/png',23 cacheControl: 3600,24 metadata: JSON.stringify({ // custom metadata passed to the user_metadata column25 yourCustomMetadata: true,26 }),27 },28 chunkSize: 6 * 1024 * 1024, // NOTE: it must be set to 6MB (for now) do not change it29 onError: function (error) {30 console.log('Failed because: ' + error)31 reject(error)32 },33 onProgress: function (bytesUploaded, bytesTotal) {34 var percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2)35 console.log(bytesUploaded, bytesTotal, percentage + '%')36 },37 onSuccess: function () {38 console.log('Download %s from %s', upload.file.name, upload.url)39 resolve()40 },41 })424344 // Check if there are any previous uploads to continue.45 return upload.findPreviousUploads().then(function (previousUploads) {46 // Found previous uploads so we select the first one.47 if (previousUploads.length) {48 upload.resumeFromPreviousUpload(previousUploads[0])49 }5051 // Start the upload52 upload.start()53 })54 })55}Upload URL
When uploading using the resumable upload endpoint, the storage server creates a unique URL for each upload, even for multiple uploads to the same path. All chunks will be uploaded to this URL using the PATCH method.
This unique upload URL will be valid for up to 24 hours. If the upload is not completed within 24 hours, the URL will expire and you'll need to start the upload again. TUS client libraries typically create a new URL if the previous one expires.
Concurrency
When two or more clients upload to the same upload URL only one of them will succeed. The other clients will receive a 409 Conflict error. Only 1 client can upload to the same upload URL at a time which prevents data corruption.
When two or more clients upload a file to the same path using different upload URLs, the first client to complete the upload will succeed and the other clients will receive a 409 Conflict error.
If you provide the x-upsert header the last client to complete the upload will succeed instead.
Uppy example
You can check a full example using Uppy.
Uppy has integrations with different frameworks:
Presigned uploads
Resumable uploads also supports using signed upload tokens to created time-limited URLs that you can share to your users by invoking the createSignedUploadUrl method on the SDK and including the returned token in the x-signature header of the resumable upload.
1// Create a signed upload URL2const { data } = await supabase.storage.from('bucket_name').createSignedUploadUrl('file_path', {3 upsert: true, // Optional: allow overwriting existing files4})56// Use the signed URL token in resumable upload headers7// Include data.token in the x-signature headerSee this full example using Uppy with signed URLs for more context.
Overwriting files
When uploading a file to a path that already exists, the default behavior is to return a 400 Asset Already Exists error.
If you want to overwrite a file on a specific path you can set the x-upsert header to true.
We do advise against overwriting files when possible, as the CDN will take some time to propagate the changes to all the edge nodes leading to stale content. Uploading a file to a new path is the recommended way to avoid propagation delays and stale content.
To learn more, see the CDN guide.