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
//删除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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值