0

const uploadFileToQiNiu = (file) => {
return new Promise((resolve, reject) => {

const { buffer, originalname } = file
const accessKey = fileConfig.qiniu.accessKey
const secretKey = fileConfig.qiniu.secretKey
const bucket = fileConfig.qiniu.bucket
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)

const options = {
  scope: bucket + ":" + originalname,
}
const putPolicy = new qiniu.rs.PutPolicy(options)
const uploadToken = putPolicy.uploadToken(mac)

const qiniuConfig = new qiniu.conf.Config()
// 空间对应的机房
qiniuConfig.zone = qiniu.zone.Zone_z2
const formUploader = new qiniu.form_up.FormUploader(qiniuConfig)
const putExtra = new qiniu.form_up.PutExtra()
const readableStream = buffer // 可读的流
formUploader.putStream(uploadToken, originalname, readableStream, putExtra, (respErr,
  respBody, respInfo) => {
  console.log(respErr)
  if (respErr) {
    console.log(respErr)
    reject()
  }
  if (respInfo.statusCode == 200) {
    console.log(respBody);
    resolve()
  } else {
    console.log(respBody);
    reject()
  }
})

})
}

感觉不会进入formUploader.putStream()方法,无论成功还是失败,没有任何响应

licc 3
2019-02-26 提问
1 个回答
0

您好,这个你们可以考虑使用 putfile 这个 func进行上传
putStream 也是没有问题的,你们重新安装下 qiniu 的 node-js sdk 试试呢

clipboard.png

const qiniu = require("qiniu");
const fs = require('fs');

let bucket = 'neiko';
let accessKey = 'IsU*yK3S87B';
let secretKey = 'qR*2t8Jmx';
let mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

//file
let localFile = "/Users/mini/Downloads/qiniu_test.jpg";

let fileReadStream = fs.createReadStream( localFile);

//overwriteKey
var keyToOverwrite = 'qiniu_test2.jpg';
var options = {
scope: bucket + ":" + keyToOverwrite
}
let putPolicy = new qiniu.rs.PutPolicy(options);
let uploadToken = putPolicy.uploadToken(mac);
console.log(uploadToken);

let config = new qiniu.conf.Config();
//注意修改zone
config.zone = qiniu.zone.Zone_z2;
config.useCdnDomain = true;

var putExtra = new qiniu.form_up.PutExtra();
putExtra.checkCrc = true

//form 上传
let formUploader = new qiniu.form_up.FormUploader(config);
formUploader.putStream(uploadToken, "qiniu_test2.jpg",fileReadStream, putExtra, function(respErr,

                                                                    respBody, respInfo) {
if (respErr) {
    console.log(respBody);
    console.log(respInfo);
    throw respErr;
}

if (respInfo.statusCode == 200) {
    console.log(respBody);
    console.log(respInfo);
} else {
    console.log(respInfo.statusCode);
    console.log(respInfo);
    console.log(respBody);
}

});

撰写答案

推广链接