node批量删除七牛云上的文件

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
const sdCdnDelete = async (req, res) => {
  let { uid } = req.body

  let bucket = 'xutongbao-static'

  let deleteOperations = [
    qiniu.rs.deleteOp(bucket, 'img/123.jpg'),
  ]
  bucketManager.batch(deleteOperations, 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.code + "\tsuccess");
          } else {
            console.log(item.code + "\t" + item.data.error);
          }
        });
      } else {
        console.log(respInfo.deleteusCode);
        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: '编辑成功',
    })
  }
}

 node批量删除七牛云上的文件_第1张图片

node批量删除七牛云上的文件_第2张图片

node批量删除七牛云上的文件_第3张图片

 https://developer.qiniu.com/kodo/1289/nodejs#rs-batch-delete

node批量删除七牛云上的文件_第4张图片

 参考链接:

https://chat.xutongbao.top/

你可能感兴趣的:(web前端,前端)