node.js 服务器 七牛云上传图片

日期: 2017-10-20         浏览量: 2843

今天做七牛云文件上传(很简单是不?)结果自己给自己挖了坑还跳的那么干脆,本想偷个懒把之前代码copy过来不就OK了,可是一直报错(上传策略设置不对很是尴尬),后来看了下官方文档好像和以前不一样了,心情不美丽啊。


不废话上代码,留着以后用,哈哈(报错在解决就是了):



// Using NPM 


$ npm install --save qiniu 




//引入qiniu模块

let qiniu = require("qiniu");


//构建上传策略函数 
function qiniuUptoken() {         
        qiniu.conf.ACCESS_KEY = "AK";         
        qiniu.conf.SECRET_KEY = "SK";     
        let bucket = "you_";     
        let putPolicy = new qiniu.rs.PutPolicy({scope: bucket});     
        return putPolicy.uploadToken(); 
}




//文件上传
function qiniuUpload(img,callback){ 
        //生成上传 Token 
        let token = qiniuUptoken(); 
        //调用uploadFile上传 
        var config = new qiniu.conf.Config();
        var formUploader = new qiniu.form_up.FormUploader(config); 
        var putExtra = new qiniu.form_up.PutExtra();
        formUploader.putFile(token, null, img, putExtra, function(respErr, respBody, respInfo) {
                callback(respErr,respBody,respInfo);
         });
} 
exports.qiniuUpload = qiniuUpload;



官方SDK地址: https://github.com/qiniu/nodejs-sdk/blob/master/examples/form_upload_simple.js