-
Notifications
You must be signed in to change notification settings - Fork 78
/
upload.js
35 lines (31 loc) · 1.02 KB
/
upload.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
const COS = require('cos-nodejs-sdk-v5');
let cos = new COS({
SecretId: process.env.TENCENT_SECRET_ID,
SecretKey: process.env.TENCENT_SECRET_KEY
});
const bucket = 'serverless-page-bucket-5z6gco4t-1257581837';
const region = 'ap-shanghai';
function uploadDirectory(directory) {
const fs = require('fs');
fs.readdirSync(directory).forEach(file => {
let fullPath = directory + '/' + file;
if (fs.lstatSync(fullPath).isDirectory()) {
uploadDirectory(fullPath);
} else {
let key = fullPath.replace('public/', '');
cos.putObject({
Bucket: bucket,
Region: region,
Key: key,
Body: fs.readFileSync(fullPath),
onProgress: function (progressData) {
console.log(progressData);
}
}, function (err, data) {
if (err) console.error(err);
console.log(data);
});
}
});
}
uploadDirectory('public');