Skip to content
On this page

Alioss

介绍

Alioss工具类,支持文件、base64数据上传

安装

sh
pnpm i @trinasolar/utils

示例

ts
import { OSS } from '@/utils/alioss/index.ts';

export default {
  props: {},
  data() {},
  setup() {
    // 获取alioss参数信息
    const res = await aliyunOssAPI()
    const { bucket, endpoint, accessKeyId, accessKeySecret, stsToken } = res
    const ossParmas = {
      region: 'oss-cn-shanghai',
      bucket,
      endpoint,
      accessKeyId,
      accessKeySecret,
      stsToken,
    }
    const ossOwnner = new OSS(ossParmas)
    // base64数据上传
    const base64Data = 'data:'image/jpeg';base64,xxxxxxxxxxxxxx'
    const base64Result = await ossOwnner.upload({ type: 'base64', payload: base64Data })
    console.log('返回生成的链接 ','base64Result.url')

    // File数据上传
    const file = new File()
    const fileResult = await ossOwnner.upload({ type: 'file', payload: file })
    console.log('返回生成的链接 ','fileResult.url')
  },
};

API

初始化

ts
const oss = new Alioss(options, future?)

options

参数必传说明类型默认值
region是Bucket所在地域,示例:'oss-cn-hangzhou'string''
bucket是存储空间名称,示例:'my-bucket-name'string''
endpoint是访问域名,示例:'oss-cn-shanghai.aliyuncs.com'string''
stsToken否临时授权stringundefined
secure否协议类型(HTTPS:true,HTTP:false)booleantrue
expiration否过期时间stringgetExpiration()
accessKeyId是秘钥 OSS_ACCESS_KEY_IDstring''
accessKeySecret是秘钥 OSS_ACCESS_KEY_SECRETstring''
env否运行环境('test'、'prod')string'test'

future(可选)获取AliossOptions方法

上传

ts
const {name, url} = oss.upload({ type = 'file', payload })
参数必传说明类型默认值
type否上传类型('file'、'base64')string'file'
payload是上传内容,由type决定(flie:File(),base64:base64数据)File或string''
参数说明类型
url返回得链接,可直接用于预览string
name文件名string

其他

方法说明参数返回值
getExpiration获取过期时间()string

参考网址

https://help.aliyun.com/zh/oss/developer-reference/installation-7?spm=a2c4g.11186623.0.i1#concept-32068-zh