forked from wcongke/upload-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
34 lines (29 loc) · 846 Bytes
/
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
const qiniu = require('qiniu')
const sha1 = require('sha1')
// 七牛提供的公钥
const accessKey = 'accessKey'
// 七牛提供的私钥
const secretKey = 'secretKey'
// 存储空间名
const bucketName = 'bucketName'
module.exports.upload = (req, res) => {
// 文件名
const fileName = `${sha1(req.query.fileName)}.${req.query.fileName.split('.').pop()}`
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const putPolicy = new qiniu.rs.PutPolicy({
scope: `${bucketName}:${fileName}`,
expires: 60 * 60 * 1,
returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
})
// 上传凭证
const uploadToken = putPolicy.uploadToken(mac)
res.send({
code: '1',
desc: 'ok',
result: {
bucketName,
fileName,
uploadToken
}
})
}