const OSS = require('ali-oss')
export function client(data) {
return new OSS({
region: 'oss-cn-shanghai', // oss-cn-shenzhen.aliyuncs.com
accessKeyId: 'LTAI4G7cccHmBUPucagmboie',
accessKeySecret: 'T3sfpC8W3Q8wGwA0yjxDt6kILO4KpP',
bucket: 'cbdleasing'
})
}
npm install ali-oss,安装依赖
import { client } from '@/api/oss'
// 上传至oss
uploadFun(file) {
const that = this
const fileName = 'risk/' + file.name + file.uid
// 定义唯一的文件名,打印出来的uid其实就是时间戳
// client 是第一步中的 client
client().multipartUpload(fileName, file.raw).then(result => {
that.importFun(result)
}).catch(err => {
that.msgError(err)
})
},
importFun(ossRep) {
const that = this
listImport({
key: ossRep.name
}).then(response => {
if (response.code === '000000') {
console.log('调用成功')
}
})
},
引入定义好的client方法,调用oss的multipartUpload方法,上传文件,成功后会返回文件路径等信息,然后就可以继续调用你们后端的方法了。