from.upload()
将一个文件上传到一个现有的bucket.
const avatarFile = event.target.files[0]
const { data, error } = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', avatarFile, {
cacheControl: '3600',
upsert: false
})
Parameters
-
path required
string
相对文件路径。应该是`folder/subfolder/filename.png`的格式。在尝试上传之前,该bucket必须已经存在。 -
fileBody required
ArrayBuffer
|ArrayBufferView
|Blob
|Buffer
|File
|FormData
|ReadableStream
|ReadableStream
|URLSearchParams
|string
要存储在bucket中的文件的主体。 -
fileOptions optional
FileOptions
HTTP headers. `cacheControl`: string, the `Cache-Control: max-age=` seconds value. `contentType`: string, the `Content-Type` header value. Should be specified if using a `fileBody` that is neither `Blob` nor `File` nor `FormData`, otherwise will default to `text/plain;charset=UTF-8`. `upsert`: boolean, whether to perform an upsert.
Notes
- 所需的策略权限:
buckets
权限: noneobjects
权限:insert
- 对于React Native,使用
Blob
、File
或FormData
都不能正常工作。使用ArrayBuffer
从base64文件数据上传文件,见下面的例子
Examples
上传文科
const avatarFile = event.target.files[0]
const { data, error } = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', avatarFile, {
cacheControl: '3600',
upsert: false
})
使用ArrayBuffer
从base64文件数据上传文件
import {decode} from 'base64-arraybuffer'
const { data, error } = await supabase
.storage
.from('avatars')
.upload('public/avatar1.png', decode('base64FileData'), {
contentType: 'image/png'
})