0

代码如下:

const se = require('./se.json')

var qiniu = require('qiniu');
var fs = require('fs');

// 初始化ak,sk
qiniu.conf.ACCESS_KEY = se.accessKey;
qiniu.conf.SECRET_KEY = se.secretKey;

//要上传的空间
bucket = 'lawlietimg';


//上传到七牛后保存的文件名
key = 'my-nodejs-logo.png';
//构建上传策略函数
function uptoken(bucket, key) {
  var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
  return putPolicy.token();
}
//生成上传 Token
token = uptoken(bucket, key);
//要上传文件的本地路径
filePath = './pic.jpeg'
//构造上传函数
function uploadFile(uptoken, key, localFile) {
  var extra = new qiniu.io.PutExtra();
    qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) {
      if(!err) {
        // 上传成功, 处理返回值
        console.log(ret.hash, ret.key, ret.persistentId);       
      } else {
        // 上传失败, 处理返回代码
        console.log(err);
      }
  });
}
//调用uploadFile上传
uploadFile(token, key, filePath);

clipboard.png

报错信息:

clipboard.png

QAQwow 231
2018-06-24 提问
3 个回答
0

PutPolicy里面应该是个对象,你点进去看看;官方demo很老了,sdk更新了,文档没有跟上节奏

0

用这个简单上传

var localFile = "/Users/jemy/Documents/qiniu.mp4";
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var key='test.mp4';
// 文件上传
formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,
  respBody, respInfo) {
  if (respErr) {
    throw respErr;
  }

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

撰写答案

你可能感兴趣的

推广链接