-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (42 loc) · 1.25 KB
/
index.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
43
const region = process.env.region;
let accessKeyId = process.env.accessKeyId;
let accessKeySecret = process.env.accessKeySecret;
const bucket = process.env.bucket;
const targetDir = process.env.targetDir || "/";
if (region && accessKeyId && accessKeySecret && bucket) {
const fs = require("fs");
const OSS = require("ali-oss");
const path = require("path");
const client = new OSS({
region,
accessKeyId,
accessKeySecret,
bucket
});
const basePath = path.resolve("./bundle");
fs.readdir(basePath, function(err, files) {
files.forEach(function(file) {
console.log(basePath + "/" + file);
const cacheControl = file.includes('.html')
? 'max-age=0, s-maxage=63072000'
: file.includes('sw.js') || file.includes('app.json')
? 'no-store'
: 'max-age=63072000'
client
.put(targetDir + file, basePath + "/" + file, {
headers: {
'Cache-Control': cacheControl
}
})
.then(result => {
console.log("put success: %j", result);
})
.catch(error => {
console.error("error: %j", error);
throw new Error("upload oss fail !");
});
});
});
} else {
throw new Error("缺少必要环境变量!");
}