node端 七牛上传成功,文件下载下来为空,0B

生成了个二维码七牛上传成功,文件下载下来为空,0B

const qrImageStream = await fs.createReadStream(path.join(__dirname, `../../imageHandler/${apkName}_qrImage.png`));
console.log(qrImageStream);

// 上传qrImage至七牛
await qiniu.putFileToQiniu(`${apkName}_qrImage.png`, qrImageStream, true);

七牛:

const accessKey = config.qiniuConfig.accessKey;
const secretKey = config.qiniuConfig.secretKey;
const bucket = config.qiniuConfig.bucket;
const publicBucketDomain = config.qiniuConfig.publicBucketDomain;
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
const qiniuConfig = new qiniu.conf.Config();
qiniuConfig.zone = qiniu.zone.Zone_z0;
const formUploader = new qiniu.form_up.FormUploader(qiniuConfig);
const putExtra = new qiniu.form_up.PutExtra();
const bucketManager = new qiniu.rs.BucketManager(mac, qiniuConfig);

/**
 * 上传七牛
 * @param  {Object}   key  [description]
 * @param readerStream
 * @param isImage
 * @return {[type]}        [description]
 */
exports.putFileToQiniu = async (key, readerStream, isImage) => {

  const options = {
    scope: bucket + ":" + key,
  };
  if (isImage) {
    options.detectMime = 1;
    options.mimeLimit = 'image/*';
  }
  const putPolicy = new qiniu.rs.PutPolicy(options);
  // 生成上传凭证
  const uploadToken = putPolicy.uploadToken(mac);
  // 上传文件
  return new Promise((resolve, reject) => {
    formUploader.putStream(uploadToken, key, readerStream, putExtra, function (respErr, respBody, respInfo) {
      if (respErr) {
        reject(respErr);
        throw respErr
      }
      if (respInfo.statusCode === 200) {
        resolve(respBody);
        console.log("success: ", key)
      }
    })
  });
};
阅读 760
评论 2018-11-22 提问
    2 个回答
    • 3
    • 新人请关照

    上传策略增加大小限制
    参考:看这里

      我把文件命名改了就ok了。。。
      后缀_qrImage.png改成_qr_image.png

        撰写回答

        登录后参与交流、获取后续更新提醒

        七牛云问答
        合作问答

        七牛云 联合 SegmentFault 共同推出技术问答专区,为开发者们提供社区支持和帮助。在这里,你将直接获得...