-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
105 lines (105 loc) · 3.11 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const getRawBody = require("raw-body");
const Core = require("@alicloud/pop-core");
exports.handler = (req, resp, context) => {
getRawBody(req, function (err, body) {
body = JSON.parse(body.toString());
if (body.token !== "<token>") {
resp.send("wrong token");
return;
}
const client = new Core({
accessKeyId: context.credentials.accessKeyId,
accessKeySecret: context.credentials.accessKeySecret,
securityToken: context.credentials.securityToken,
endpoint: "https://cdn.aliyuncs.com",
apiVersion: "2018-05-10",
});
if (body.action == "purge") {
console.log(
`purge ${body.isFolder ? "Directory" : "File"} path:${body.path.join(
"\r\n"
)}`
);
client
.request(
"RefreshObjectCaches",
{
RegionId: "cn-hangzhou",
ObjectPath: body.path.map(v => {
if (v.startsWith("//")) {
v = "https:" + v;
}
return v;
}).join("\r\n"),
ObjectType: body.isFolder ? "Directory" : "File",
},
{
method: "POST",
}
)
.then(
(result) => {
console.log(JSON.stringify(result));
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(result, null, " "));
},
(ex) => {
console.log(ex);
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(ex, null, " "));
}
);
}
if (body.action == "Quota") {
console.log(`quota`);
client
.request(
"DescribeRefreshQuota",
{
RegionId: "cn-hangzhou",
},
{
method: "POST",
}
)
.then(
(result) => {
console.log(JSON.stringify(result));
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(result, null, " "));
},
(ex) => {
console.log(ex);
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(ex, null, " "));
}
);
}
if (body.action == "Tasks") {
console.log(`tasks`);
client
.request(
"DescribeRefreshTasks",
{
RegionId: "cn-hangzhou",
ResourceGroupId: " rg-aek2mcogz6aphiy",
},
{
method: "POST",
}
)
.then(
(result) => {
console.log(JSON.stringify(result));
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(result, null, " "));
},
(ex) => {
console.log(ex);
resp.setHeader('content-type', 'application/json');
resp.send(JSON.stringify(ex, null, " "));
}
);
}
});
};