标准上传
Learn how to upload files to Supabase Storage.
正在上传 #
🌐 Uploading
标准文件上传方法很适合小文件,大小不超过6MB。
🌐 The standard file upload method is ideal for small files that are not larger than 6MB.
你可以用 supabase-js SDK 按传统的 multipart/form-data 格式实现文件上传。下面是一个使用标准上传方法上传文件的例子:
🌐 You implement file uploads with the supabase-js SDK using the traditional multipart/form-data format. Here's an example of how to upload a file using the standard upload method:
虽然你可以使用标准上传方式上传最多5GB的文件,但我们建议使用TUS 可续传上传来上传大于6MB的文件,以获得更好的可靠性。
🌐 Though you can upload up to 5GB files using the standard upload method, we recommend using TUS Resumable Upload for uploading files greater than 6MB in size for better reliability.
1// @noImplicitAny: false23// ---cut---4import { createClient } from '@supabase/supabase-js'56// Create Supabase client7const supabase = createClient('your_project_url', 'your_supabase_api_key')89// Upload file using standard upload10async function uploadFile(file) {11 const { data, error } = await supabase.storage.from('bucket_name').upload('file_path', file)12 if (error) {13 // Handle error14 } else {15 // Handle success16 }17}正在覆盖文件 #
🌐 Overwriting files
当你上传一个已经存在的路径文件时,默认行为是返回一个 400 Asset Already Exists 错误。如果你想覆盖某个路径上的文件,可以将 upsert 选项设置为 true,或者使用 x-upsert 头。
🌐 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 upsert options to true or using the x-upsert header.
1import { createClient } from '@supabase/supabase-js'23const file = new Blob()45// ---cut---6// Create Supabase client7const supabase = createClient('your_project_url', 'your_supabase_api_key')89await supabase.storage.from('bucket_name').upload('file_path', file, {10 upsert: true,11})我们确实建议尽量避免覆盖文件,因为我们的内容分发网络(CDN)需要一些时间才能将更改传播到所有边缘节点,这可能会导致内容过时。 将文件上传到新的路径是避免传播延迟和内容过时的推荐方式。
🌐 We do advise against overwriting files when possible, as our Content Delivery Network will take sometime 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.
内容类型 #
🌐 Content type
默认情况下,Storage 会根据文件扩展名来判断资源的内容类型。如果你想为资源指定内容类型,可以在上传时传入 contentType 选项。
🌐 By default, Storage will assume the content type of an asset from the file extension. If you want to specify the content type for your asset, pass the contentType option during upload.
1import { createClient } from '@supabase/supabase-js'23const file = new Blob()45// ---cut---6// Create Supabase client7const supabase = createClient('your_project_url', 'your_supabase_api_key')89await supabase.storage.from('bucket_name').upload('file_path', file, {10 contentType: 'image/jpeg',11})并发 #
🌐 Concurrency
当两个或更多的客户端上传文件到同一路径时,第一个完成上传的客户端会成功,而其他客户端会收到 400 Asset Already Exists 错误。
如果你提供了 x-upsert 头信息,则最后一个完成上传的客户端会成功。
🌐 When two or more clients upload a file to the same path, the first client to complete the upload will succeed and the other clients will receive a 400 Asset Already Exists error.
If you provide the x-upsert header the last client to complete the upload will succeed instead.