forked from queryfish/puppet_books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveToAliOSS.js
42 lines (38 loc) · 1.32 KB
/
saveToAliOSS.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
42
const OSS = require('ali-oss');
const client = new OSS({
region: 'oss-cn-beijing',
//云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,部署在服务端使用RAM子账号或STS,部署在客户端使用STS。
accessKeyId:'LTAI4GJFGjPc5nTz4fvKp64y',
accessKeySecret:'uEwFR131y2MUD0qnmmsnXmIbVpzteN',
bucket: 'stephen-s-bookstore'
});
exports.putPromise = function(local, remote, options){
return client.put(remote, local, options);
};
exports.put =
async function(localfile, remotefil) {
try {
// object表示上传到OSS的Object名称,localfile表示本地文件或者文件路径
let r1 = await client.put(remotefile,localfile,{timeout:3600000});
console.log('put success: %j', r1);
return 0;
// let r2 = await client.get('object');
// console.log('get success: %j', r2);
} catch(e) {
// console.error('error: %j', e);
console.log(e);
return 1;
}
}
async function get() {
try {
// object表示上传到OSS的Object名称,localfile表示本地文件或者文件路径
let r1 = await client.get('test/test.mobi','./books/test2.mobi');
console.log('put success: %j', r1);
// let r2 = await client.get('object');
// console.log('get success: %j', r2);
} catch(e) {
// console.error('error: %j', e);
console.log(e);
}
}