From 6ab51e708159ab1b51655f1b837b33d214981b64 Mon Sep 17 00:00:00 2001 From: SkyAo Date: Wed, 8 Mar 2017 09:55:55 +0800 Subject: [PATCH] update cos: add buffer upload --- qcloud_cos/.gitignore | 1 + qcloud_cos/lib/cos.js | 58 +++++++++++++++++++++++++++++++++++++++ qcloud_cos/test/sample.js | 3 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 qcloud_cos/.gitignore diff --git a/qcloud_cos/.gitignore b/qcloud_cos/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/qcloud_cos/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/qcloud_cos/lib/cos.js b/qcloud_cos/lib/cos.js index 39ae267..b6c4938 100644 --- a/qcloud_cos/lib/cos.js +++ b/qcloud_cos/lib/cos.js @@ -147,6 +147,63 @@ function upload(filePath, bucket, dstpath, bizattr, insertOnly, callback) { } } +function put(fileBuffer, bucket, dstpath, bizattr, insertOnly, callback) { + if (typeof bizattr === 'function') { + callback = bizattr; + bizattr = null; + }else if(typeof insertOnly === 'function'){ + callback = insertOnly; + insertOnly = undefined; + } else { + callback = callback || function(ret){ console.log(ret); }; + } + + if (typeof callback === 'function' && fileBuffer instanceof Buffer) { + + bucket = bucket.strip(); + dstpath = fixPath(dstpath); + var expired = parseInt(Date.now() / 1000) + conf.EXPIRED_SECONDS; + var sign = auth.signMore(bucket, expired); + var url = generateResUrl(bucket, dstpath); + var urlInfo = urlM.parse(url); + + var sha = crypto.createHash('sha1'); + sha.update(fileBuffer); + + var form = formstream() + .field('op', 'upload') + .field('sha', sha.digest('hex')); + + // TODO: formsteam.buffer 可以编辑 buffer + form.buffer('filecontent', fileBuffer, dstpath); + + if (bizattr) { + form.field('biz_attr', bizattr.toString()); + } + if(insertOnly!==undefined){ + form.field('insertOnly', insertOnly); + } + + var headers = form.headers(); + headers['Authorization'] = sign; + headers['User-Agent'] = conf.USER_AGENT(); + var options = { + protocol: urlInfo.protocol, + hostname: urlInfo.hostname, + port: urlInfo.port, + path: urlInfo.path, + method: 'POST', + headers: headers + }; + + var req = buildRequest(options, callback); + req && form.pipe(req); + } else { + // error, file not exists + callback({'code':COS_PARAMS_ERROR, 'message':'file '+filePath+' not exists or params error', 'data':{}}); + } +} + /** * 分片上传获取size * @param {int} size 文件分片大小,Bytes @@ -915,3 +972,4 @@ exports.list = list; exports.prefixSearch = prefixSearch; exports.createFolder = createFolder; exports.moveFile = moveFile; +exports.put = put; diff --git a/qcloud_cos/test/sample.js b/qcloud_cos/test/sample.js index 36c1093..6b48cbd 100644 --- a/qcloud_cos/test/sample.js +++ b/qcloud_cos/test/sample.js @@ -6,8 +6,9 @@ qcloud_cos.conf.setAppInfo('1000000', 'AKIiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', 'w qcloud_cos.cos.deleteFile('bucket01', '123/t.mp4', function(ret) {console.log(ret); qcloud_cos.cos.upload('./test', 'bucket01', '123/t.mp4', '0666', function(ret) {console.log(ret); qcloud_cos.cos.updateFile('bucket01', '123/t.mp4', '', function(ret) {console.log(ret); +qcloud_cos.cos.put(new Buffer('123'), '123/t.txt', '', function(ret) {console.log(ret); qcloud_cos.cos.statFile('bucket01', '123/t.mp4', function(ret) {console.log(ret); qcloud_cos.cos.prefixSearch('bucket01', '123', 'z', function(ret) {console.log(ret); qcloud_cos.cos.createFolder('bucket01', '/123', function(ret) {console.log(ret); qcloud_cos.cos.deleteFolder('bucket01', '123/', function(ret) {console.log(ret); -qcloud_cos.cos.upload_slice('./test', 'bucket01', '123/t.mp4', '0666', 512000);});});});});});});}); +qcloud_cos.cos.upload_slice('./test', 'bucket01', '123/t.mp4', '0666', 512000);});});});});});});});});