这个是一个Vue中用阿里云提供的SDK实现上传文件至OSS的demo,只有功能实现,没有美观度可言,希望给大家提供一点帮助

1.安装依赖

1
npm install ali-oss --save

2.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<template>
<div id="app">
<input type="file" @change="getInfo($event)">
<!-- 文件路径显示-->
<h2>path:{{ path }}</h2>
<button @click="put">上传</button>
</div>
</template>

<script>
const oss = require('ali-oss');
const client = oss({//设置OSS参数,输入自己的accessKeyId、accessKeySecret、bucket和region
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
region: 'your region'
});

export default {
name: 'App',
data() {
return {
path: '',
OSSPath: 'test/',//要上传至OSS的路径
file: {}
}
},
methods: {
getInfo(e) {//选择文件
this.path = e.target.value;
this.file = e.target.files[0]
},
async put() {
if (this.path !== '') {//判断文件非空
let textArray=this.path.split('.')
let extension = '.' + textArray[textArray.length-1]//获取扩展名
try {
let result = await client.put(this.OSSPath + Date.parse(new Date()) + extension, this.file);//此处文件命名使用的为时间戳防止冲突,可自行修改规则
console.log(result);
console.log(result.url);//完整的文件url
alert('上传成功')
} catch (e) {
console.log(e);
alert("上传失败")
}
}
else{
alert("请先选择要上传文件")
}
}
}
}
</script>

<style>
</style>