const qiniu = require('qiniu')
const {
getQiNiuKey,
} = require('./tools')
//#region 七牛云
const { accessKey, secretKey } = getQiNiuKey()
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
let config = new qiniu.conf.Config()
// 空间对应的机房
config.zone = qiniu.zone.Zone_z1
const putPolicy = new qiniu.rs.PutPolicy({
//scope: 'xutongbao-video',
scope: 'xutongbao-static',
returnBody: `{
"code": 200,
"data": {"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","file":"$(fname)"},
"message": "成功"
}`,
expires: 3600 * 24 * 30, //一个月
})
const uploadToken = putPolicy.uploadToken(mac)
let formUploader = new qiniu.form_up.FormUploader(config)
let putExtra = new qiniu.form_up.PutExtra()
putExtra.checkCrc = false
let bucketManager = new qiniu.rs.BucketManager(mac, config);
module.exports = {
uploadToken,
formUploader,
putExtra,
bucketManager
}
//#endregion
//删除CDN文件
const sdCdnDelete = async (req, res) => {
let { uid } = req.body
var bucket = 'xutongbao-static'
var key = 'img/123.jpg'
bucketManager.stat(bucket, key, function (err, respBody, respInfo) {
if (err) {
console.log(err)
//throw err;
} else {
if (respInfo.statusCode == 200) {
console.log(respBody.hash)
console.log(respBody.fsize)
console.log(respBody.mimeType)
console.log(respBody.putTime, moment(respBody.putTime / 10000))
console.log(respBody.type)
} else {
console.log(respInfo.statusCode)
console.log(respBody.error)
}
}
})
const now = Date.now()
const editObj = {
updateTime: now,
}
let { editStr } = getSqlUpdateStr({ editObj })
const err = await runSql(`UPDATE sd SET ${editStr} WHERE uid = '${uid}'`)
if (err) {
res.send({
code: 400,
data: err,
message: '编辑失败',
})
} else {
await refreshRedis({ tableName: 'sd' })
res.send({
code: 200,
data: {
uid,
},
message: '编辑成功',
})
}
}
https://developer.qiniu.com/kodo/1289/nodejs#rs-batch-delete
批量查询:
const sdCdnDelete = async (req, res) => {
let { uid } = req.body
let bucket = 'xutongbao-static'
let statOperations = [
qiniu.rs.statOp(bucket, 'img/123.jpg'),
qiniu.rs.statOp(bucket, 'img/logo.png'),
]
bucketManager.batch(statOperations, function (err, respBody, respInfo) {
if (err) {
console.log(err)
//throw err;
} else {
// 200 is success, 298 is part success
if (parseInt(respInfo.statusCode / 100) == 2) {
respBody.forEach(function (item) {
if (item.code == 200) {
console.log(
item.data.fsize +
'\t' +
item.data.hash +
'\t' +
item.data.mimeType +
'\t' +
item.data.putTime +
'\t' +
item.data.type
)
console.log(item.data.putTime, moment(item.data.putTime / 10000))
} else {
console.log(item.code + '\t' + item.data.error)
}
})
} else {
console.log(respInfo.statusCode)
console.log(respBody)
}
}
})
const now = Date.now()
const editObj = {
updateTime: now,
}
let { editStr } = getSqlUpdateStr({ editObj })
const err = await runSql(`UPDATE sd SET ${editStr} WHERE uid = '${uid}'`)
if (err) {
res.send({
code: 400,
data: err,
message: '编辑失败',
})
} else {
await refreshRedis({ tableName: 'sd' })
res.send({
code: 200,
data: {
uid,
},
message: '编辑成功',
})
}
}
参考链接:
https://developer.qiniu.com/kodo/1289/nodejs#rs-batch-delete
https://chat.xutongbao.top/