安装
npm install ali-oss
引入 uploadAndDeleteOss.js
const OSS = require('ali-oss')
import { getAliOssUpload } from '@/api/common'
上传
export async function uploadOSS(file) {
const { data } = await getAliOssUpload()
console.log(data)
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: data.AccessKeyId,
accessKeySecret: data.AccessKeySecret,
stsToken: data.SecurityToken,
bucket: 'yangdev',
refreshSTSToken: async() => {
const info = await getAliOssUpload()
return {
accessKeyId: info.data.AccessKeyId,
accessKeySecret: info.data.AccessKeySecret,
stsToken: info.SecurityToken
}
},
refreshSTSTokenInterval: 300000
})
const result = client.put(`/yang/${new Date().getTime() + file.name}`, file.raw || file)
return result
}
删除
export async function delOSS(config, objKey) {
const { data } = await getAliOssUpload()
console.log(data)
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: data.AccessKeyId,
accessKeySecret: data.AccessKeySecret,
stsToken: data.SecurityToken,
bucket: 'yangdev',
refreshSTSToken: async() => {
const info = await getAliOssUpload()
return {
accessKeyId: info.data.AccessKeyId,
accessKeySecret: info.data.AccessKeySecret,
stsToken: info.SecurityToken
}
},
refreshSTSTokenInterval: 300000
})
console.log(objKey)
const result = client.delete(objKey)
return result
}
封装element的upload组件 ImageUpload.vue
<template>
<div class="page">
<div v-viewer="optionsImage">
<el-upload
action="#"
:class="{ hideUploadBtn: hide }"
:file-list="fileList"
:limit="limitCount"
list-type="picture-card"
:http-request="upload"
:before-upload="beforeUploadFile"
:on-success="handleSuccess"
>
<i slot="default" class="el-icon-plus" />
<div slot="file" slot-scope="{ file }" style="position: relative">
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
<span class="el-upload-list__item-delete" @click="handleRemove(file)">
<i class="el-icon-delete" />
</span>
</div>
</el-upload>
</div>
</div>
</template>
<script>
import { uploadOSS, delOSS } from '@/utils/uploadAndDeleteOss'
export default {
components: {},
model: {
prop: 'images',
event: 'change'
},
props: {
images: {
type: Array,
default: () => {
return []
}
}
},
data() {
return {
optionsImage: {
inline: false
},
fileList: [],
limitCount: 6,
hide: false,
maxSize: 5 * 1024 * 1024,
imgOssList: [],
imgOssName: [],
fileNameList: []
}
},
computed: {},
watch: {},
created() {},
mounted() {},
beforeCreate() {},
beforeMount() {},
beforeUpdate() {},
updated() {},
beforeDestroy() {},
destroyed() {},
activated() {},
methods: {
async upload(file) {
try {
const imgObj = await uploadOSS(file.file)
console.log(imgObj)
this.imgOssName.push(imgObj.name)
this.fileNameList.push(file.file.name)
if (imgObj.url) {
this.imgOssList.push(imgObj.url)
this.$emit('change', this.imgOssList)
}
} catch (err) {
this.$message({
type: 'error',
message: '上传失败,请稍后再试'
})
console.log(err)
}
},
beforeUploadFile(file) {
return new Promise((resolve, reject) => {
if (file.size > this.maxSize) {
this.$message({
type: 'error',
message: '单张支付凭证不能大于5M'
})
return reject(false)
} else {
return resolve(true)
}
})
},
handleSuccess(res, file, fileList) {
this.hide = fileList.length >= this.limitCount
this.fileList = fileList
},
handleRemove(file) {
const index = this.fileNameList.indexOf(file.name)
this.fileList.splice(index, 1)
this.imgOssList.splice(index, 1)
this.hide = this.fileList.length >= this.limitCount
delOSS(this.ossConfig, this.imgOssName[index])
}
}
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.el-upload-list__item-delete {
display: block;
font-size: 20px;
}
</style>
使用
<ImageUpload v-model="images">