Skip to content

update cos: add buffer upload #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qcloud_cos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
58 changes: 58 additions & 0 deletions qcloud_cos/lib/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -915,3 +972,4 @@ exports.list = list;
exports.prefixSearch = prefixSearch;
exports.createFolder = createFolder;
exports.moveFile = moveFile;
exports.put = put;
3 changes: 2 additions & 1 deletion qcloud_cos/test/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);});});});});});});});});