用了jsSDK和nodeSDK,nodeSDK返回jsSDK需要的token,上传一直报403 (Forbidden)错误,
存储空间用的是华南区域的,地址也对应了,
附上代码
前台:
let uploader = Qiniu.uploader({
runtimes: 'html5,flash,html4',
browse_button: 'upload',
uptoken_url: './videos/upToken',
get_new_uptoken: true,
domain: 'http://on5gjg7q0.bkt.clouddn.com',
max_file_size: '100mb',
max_retries: 1,
chunk_size: '10mb',
auto_start: true,
unique_names: true,
init: {
'UploadProgress': function (up, file) {
// 每个文件上传时,处理相关的事情
let chunk_size = plupload.parseSize(this.getOption('chunk_size'));
console.log(chunk_size);
app.speed = Math.round(file.speed / 1024) + 'KB/S';
app.percentage = file.percent;
},
'FileUploaded': function (up, file, info) {
let domain = up.getOption('domain');
let res = $.parseJSON(info);
let sourceLink = domain + "/" + res.key;
console.log(sourceLink);
},
'Error': function (up, err, errTip) {
//上传出错时,处理相关的事情
console.log()
},
'UploadComplete': function () {
//队列文件处理完毕后,处理相关的事情
}
}
});
后台
upToken: function (req, res, next) {
let putPolicy = new qiniu.rs.PutPolicy(bucket + ":" + key);
//putPolicy.callbackUrl = 'http://your.domain.com/callback';
//putPolicy.callbackBody = 'filename=$(fname)&filesize=$(fsize)';
res.json({"uptoken": putPolicy.token()});
console.log(putPolicy.token());
return putPolicy.token();
},
浏览器控制台报错:
如何解决?
自己解决了,主要有两点,第一:前段js-sdk的‘domain’字段填的是存储空间的域名,而后端node-sdk返回token的‘bucket’字段填的是存储空间名。第二:报错的内容为‘key doesn't match with scope’就是前后端的key值不匹配,七牛官方node demo中默认包含key,即
let putPolicy = new qiniu.rs.PutPolicy(bucket + ":" + key);
这句代码,但是js-sdk中却没有定义key,两者不匹配,所以报错,解决办法把后端改成let putPolicy = new qiniu.rs.PutPolicy(bucket);
即可