七牛云储存 nodejs qiniu 模块
测试的一个实例
const qiniu = require(‘qiniu‘)
// 需要填写你的 Access Key 和 Secret Key
var accessKey = ‘‘
var secretKey = ‘‘
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
// 要上传的空间名buket
var bucket = ‘image‘
var options = {
scope: bucket,
callbackBody: ‘{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}‘,
callbackBodyType: ‘application/json‘
}
var putPolicy = new qiniu.rs.PutPolicy(options)
var uploadToken = putPolicy.uploadToken(mac)
var config = new qiniu.conf.Config()
// 是否使用https域名
// config.useHttpsDomain = true
// 上传是否使用cdn加速
// config.useCdnDomain = true
var localFile = ‘./server/logo.png‘
var formUploader = new qiniu.form_up.FormUploader(config)
var putExtra = new qiniu.form_up.PutExtra()
var key = ‘my-node-logo.png‘
// 文件上传
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr, respBody, respInfo) {
console.log()
if (respInfo.statusCode === 200) {
console.log(respInfo)
console.log(‘1‘, respBody)
} else {
console.log(respInfo.statusCode)
console.log(‘2‘, respBody)
}
})