-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALiYunOS.js
41 lines (39 loc) · 1.05 KB
/
ALiYunOS.js
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
/**
* 获取阿里云client配置 需要ali-oss 插件
* @param {Object} config 阿里云配置
* @param {Number} timeout 超时时间, 默认为 60000
* @returns {Promise<Client>}
*/
const getOssClient = async function (config, timeout = 60000) {
const OSS = require('ali-oss')
const ossConfig = {
accessKeyId: config.accessKeyId,
accessKeySecret: config.accessKeySecret,
bucket: config.bucketName,
endpoint: 'oss-cn-shenzhen.aliyuncs.com',
stsToken: config.securityToken,
region: 'oss-cn-shenzhen',
timeout: timeout
}
return new OSS(ossConfig)
}
/**
* 完整上传
* @param bufferArr 文件
* @param timeout 超时时间, 默认为 60000
* @param config 阿里云配置
* @returns {Promise<*>}
*/
const uploadFile = async function (bufferArr, config, timeout = 60000) {
try {
const client = await getOssClient(config, timeout)
return await client.put(config.objectKey, bufferArr)
} catch (e) {
console.error('完整上传文件失败', e)
throw e
}
}
export default {
getOssClient,
uploadFile
}