信息配置html
const qiniu = require('qiniu')
// 須要填寫你的 Access Key 和 Secret Key var accessKey = '' var secretKey = '' var mac = new qiniu.auth.digest.Mac(accessKey, secretKey) // 要上傳的空間 var bucket = 'image' var options = { scope: bucket, callbackBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}', callbackBodyType: 'application/json' } var putPolicy = new qiniu.rs.PutPolicy(options) var uploadToken = putPolicy.uploadToken(mac) var config = new qiniu.conf.Config() // 是否使用https域名 // config.useHttpsDomain = true // 上傳是否使用cdn加速 // config.useCdnDomain = true ** *在這裏添加下面的方法 *
文件路徑formUploader.putFile方法node
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); } });
文件字節方法formUploader.putjson
** 該方法七牛不支持 buffer類,可以使用Buffer.toString()進行轉換api
var formUploader = new qiniu.form_up.FormUploader(config); var putExtra = new qiniu.form_up.PutExtra(); var key='test.txt'; formUploader.put(uploadToken, key, "hello world", putExtra, function(respErr, respBody, respInfo) { if (respErr) { throw respErr; } if (respInfo.statusCode == 200) { console.log(respBody); } else { console.log(respInfo.statusCode); console.log(respBody); } });
可讀流上傳方式formUploader.putStreamapp
** 最易想到的就是fs.createReadStream 建立可讀流對象,http類,以及child進程輸出,輸入流、更多瞭解查閱nodejs stream類ui
var formUploader = new qiniu.form_up.FormUploader(config); var putExtra = new qiniu.form_up.PutExtra(); var readableStream = xxx; // 可讀的流 formUploader.putStream(uploadToken, key, readableStream, putExtra, function(respErr, respBody, respInfo) { if (respErr) { throw respErr; } if (respInfo.statusCode == 200) { console.log(respBody); } else { console.log(respInfo.statusCode); console.log(respBody); } });
斷點續傳resumeUploader.putFile調用一個新的resumeUploader.putStream對象spa
var localFile = "/Users/jemy/Documents/qiniu.mp4"; var resumeUploader = new qiniu.resume_up.ResumeUploader(config); var putExtra = new qiniu.resume_up.PutExtra(); // 擴展參數 putExtra.params = { "x:name": "", "x:age": 27, } putExtra.fname = 'testfile.mp4'; // 若是指定了斷點記錄文件,那麼下次會從指定的該文件嘗試讀取上次上傳的進度,以實現斷點續傳 putExtra.resumeRecordFile = 'progress.log'; var key = null; // 文件分片上傳 resumeUploader.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); } });
resumeUploader.putWithoutKey 調用 key爲null的 resumeUploader.put方法
resumeUploader.putFileWithoutKey 調用 key爲null的 resumeUploader.putFile方法code