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
  })

参数

  • path required string

    相对文件路径。应该是`folder/subfolder/filename.png`的格式。在尝试上传之前,该bucket必须已经存在。
  • fileBody required ArrayBuffer | ArrayBufferView | Blob | Buffer | File | FormData | ReadableStream | ReadableStream | URLSearchParams | string

    要存储在bucket中的文件的主体。
  • 文件选项 optional FileOptions

    请求头
    cacheControl the `Cache-Control: max-age=` seconds value. | string
    contentType 'Content-Type', 如果使用的'fileBody'既不是'Blob',也不是'File'或'FormData',则应指定该参数,否则将默认为'text/plain'。字符集=UTF-8 '。 | string
    upsert 是否执行更新插入。 | boolean

提示

  • 所需的策略权限:
    • buckets 权限: none
    • objects 权限: insert
  • 对于React Native,使用BlobFileFormData都不能正常工作。使用ArrayBuffer从base64文件数据上传文件,见下面的例子

例子

上传文科

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'
  })

results matching ""

    No results matching ""