七牛雲儲存 nodejs qiniu 模塊 信息配置 文件路徑formUploader.putFile方法 文件位元組方法formUploader.put ** 該方法七牛不支持 buffer類,可使用Buffer.toString()進行轉換 可讀流上傳方式formUploader.putStrea ...
七牛雲儲存 nodejs qiniu 模塊
信息配置
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方法
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.put
** 該方法七牛不支持 buffer類,可使用Buffer.toString()進行轉換
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.putStream
** 最易想到的就是fs.createReadStream 創建可讀流對象,http類,以及child進程輸出,輸入流、更多瞭解查閱nodejs stream類
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對象
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方法