Skip to content
Storage

标准上传

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:

1
// @noImplicitAny: false
2
3
// ---cut---
4
import { createClient } from '@supabase/supabase-js'
5
6
// Create Supabase client
7
const supabase = createClient('your_project_url', 'your_supabase_api_key')
8
9
// Upload file using standard upload
10
async function uploadFile(file) {
11
const { data, error } = await supabase.storage.from('bucket_name').upload('file_path', file)
12
if (error) {
13
// Handle error
14
} else {
15
// Handle success
16
}
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.

1
import { createClient } from '@supabase/supabase-js'
2
3
const file = new Blob()
4
5
// ---cut---
6
// Create Supabase client
7
const supabase = createClient('your_project_url', 'your_supabase_api_key')
8
9
await 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.

1
import { createClient } from '@supabase/supabase-js'
2
3
const file = new Blob()
4
5
// ---cut---
6
// Create Supabase client
7
const supabase = createClient('your_project_url', 'your_supabase_api_key')
8
9
await 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.