forked from unliar/oss-element-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
75 lines (72 loc) · 2.15 KB
/
router.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
const express = require("express")
const router = express.Router()
const crypto = require("crypto")
const config = {
dirPath: 'oss/file/', //oss 文件夹 不存在会自动创建
bucket: 'glhtest', //oss应用名
region: 'oss-cn-hangzhou', //oss节点名
accessKeyId: '不给看', //申请的osskey
accessKeySecret: '不给看', //申请的osssecret
callbackIp: "116.62.102.2", //回调ip,一定要能被外网访问的地址,你可以暂时用这个...后台的代码和下面路由一致,不过不建议
callbackPort: "2225", //回调端口
callbackPath: "acapi/be/ossCallback", //回调接口
expAfter: 60000, //签名失效时间
maxSize: 1048576000 //最大文件大小
}
router.get("/ossSign", (req, res) => {
const {
bucket,
region,
expAfter,
maxSize,
dirPath,
accessKeyId,
accessKeySecret,
callbackIp,
callbackPort,
callbackPath
} = config
const host = `http://${bucket}.${region}.aliyuncs.com` //你的oss完整地址
const expireTime = new Date().getTime() + expAfter
const expiration = new Date(expireTime).toISOString()
const policyString = JSON.stringify({
expiration,
conditions: [
['content-length-range', 0, maxSize],
['starts-with', '$key', dirPath]
]
})
const policy = Buffer(policyString).toString("base64")
const Signature = crypto.createHmac('sha1', accessKeySecret).update(policy).digest("base64")
const callbackBody = {
"callbackUrl": `http://${callbackIp}:${callbackPort}/${callbackPath}`,
"callbackHost": `${callbackIp}`,
"callbackBody": "{\"filename\": ${object},\"size\": ${size}}",
"callbackBodyType": "application/json"
}
const callback = Buffer(JSON.stringify(callbackBody)).toString('base64')
res.json({
statusCode: 200,
message: 'oss签名成功',
result: {
Signature,
policy,
host,
'OSSAccessKeyId': accessKeyId,
'key': expireTime,
'success_action_status': 200,
dirPath,
callback
}
})
})
router.post("/ossCallback", (req, res, next) => {
res.json({
statusCode: 200,
message: 'oss参数回调',
result: {
...req.body
}
})
})
module.exports = router