Skip to content

Commit

Permalink
支持多版本
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Mar 6, 2018
1 parent e44abf6 commit 8d12e9b
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 218 deletions.
74 changes: 70 additions & 4 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function putBucketCors() {
"AllowedOrigin": ["*"],
"AllowedMethod": ["GET", "POST", "PUT", "DELETE", "HEAD"],
"AllowedHeader": ["*"],
"ExposeHeader": ["ETag"],
"ExposeHeader": ["ETag", "x-cos-acl", "x-cos-version-id", "x-cos-delete-marker", "x-cos-server-side-encryption"],
"MaxAgeSeconds": "5"
}]
}
Expand Down Expand Up @@ -255,12 +255,12 @@ function putBucketLifecycle() {
"Rules": [{
'ID': 1,
'Filter': {
'Prefix': 'test123',
'Prefix': 'cas',
},
'Status': 'Enabled',
'Transition': {
'Date': '2016-10-31T00:00:00+08:00',
'StorageClass': 'STANDARD_IA'
'Days': 0,
'StorageClass': 'ARCHIVE'
}
}]
}
Expand Down Expand Up @@ -309,6 +309,16 @@ function getBucketVersioning() {
});
}

function listObjectVersions() {
cos.listObjectVersions({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Prefix: "1mb.zip"
}, function (err, data) {
console.log(err || JSON.stringify(data, null, ' '));
});
}

function putBucketReplication() {
var AppId = config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1);
cos.putBucketReplication({
Expand Down Expand Up @@ -505,6 +515,22 @@ function deleteMultipleObject() {
});
}

function restoreObject() {
cos.restoreObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1.txt',
RestoreRequest: {
Days: 1,
CASJobParameters: {
Tier: 'Expedited'
}
}
}, function (err, data) {
console.log(err || data);
});
}

function abortUploadTask() {
cos.abortUploadTask({
Bucket: config.Bucket, /* 必须 */ // Bucket 格式:test-1250000000
Expand Down Expand Up @@ -534,6 +560,9 @@ function sliceUploadFile() {
Region: config.Region,
Key: filename, /* 必须 */
FilePath: filepath, /* 必须 */
Headers: {
'test': '123',
},
TaskReady: function (tid) {
TaskId = tid;
},
Expand Down Expand Up @@ -565,6 +594,40 @@ function restartTask() {
console.log('restart');
}

function uploadFiles() {
var filename = 'mb.zip';
var blob = util.createFile({size: 1024 * 1024 * 10});
cos.uploadFiles({
files: [{
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1' + filename,
Body: blob,
}, {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '2' + filename,
Body: blob,
}, {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '3' + filename,
Body: blob,
}],
SliceSize: 1024 * 1024,
onProgress: function (info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
},
onFileFinish: function (err, data, options) {
console.log(options.Key + ' 上传' + (err ? '失败' : '完成'));
},
}, function (err, data) {
console.log(err || data);
});
}

getService();
// getAuth();
// getObjectUrl();
Expand All @@ -586,6 +649,7 @@ getService();
// putBucketLifecycle();
// deleteBucketLifecycle();
// getBucketVersioning();
// listObjectVersions();
// putBucketVersioning();
// getBucketReplication();
// putBucketReplication();
Expand All @@ -599,8 +663,10 @@ getService();
// getObjectAcl();
// deleteObject();
// deleteMultipleObject();
// restoreObject();
// abortUploadTask();
// sliceUploadFile();
// cancelTask();
// pauseTask();
// restartTask();
// uploadFiles();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-nodejs-sdk-v5",
"version": "2.2.6",
"version": "2.3.0",
"description": "cos nodejs sdk v5",
"main": "index.js",
"scripts": {
Expand Down
95 changes: 93 additions & 2 deletions sdk/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function sliceUploadFile(params, callback) {
Bucket: Bucket,
Region: Region,
Key: Key,
Headers: params.Headers,
StorageClass: StorageClass,
FilePath: FilePath,
FileSize: FileSize,
Expand Down Expand Up @@ -208,7 +209,7 @@ function getUploadIdAndPartList(params, callback) {
return callback(errData);
});

// 不存在 UploadId
// 存在 UploadId
ep.on('upload_id_ready', function (UploadData) {
// 转换成 map
var map = {};
Expand Down Expand Up @@ -242,6 +243,7 @@ function getUploadIdAndPartList(params, callback) {
Bucket: Bucket,
Region: Region,
Key: Key,
Headers: params.Headers,
StorageClass: StorageClass,
});
self.multipartInit(_params, function (err, data) {
Expand Down Expand Up @@ -418,8 +420,8 @@ function uploadSliceList(params, cb) {
SliceSize: SliceSize,
FileSize: FileSize,
PartNumber: PartNumber,
ServerSideEncryption: ServerSideEncryption,
FilePath: FilePath,
ServerSideEncryption: ServerSideEncryption,
UploadData: UploadData,
onProgress: function (data) {
FinishSize += data.loaded - preAddSize;
Expand Down Expand Up @@ -566,6 +568,7 @@ function abortUploadTask(params, callback) {
Bucket: Bucket,
Region: Region,
Key: Key,
Headers: params.Headers,
AsyncLimit: AsyncLimit,
AbortArray: AbortArray
}, function (err, data) {
Expand Down Expand Up @@ -638,6 +641,7 @@ function abortUploadTaskArray(params, callback) {
Bucket: Bucket,
Region: Region,
Key: AbortItem.Key,
Headers: params.Headers,
UploadId: UploadId
}, function (err, data) {
var task = {
Expand Down Expand Up @@ -678,9 +682,96 @@ function abortUploadTaskArray(params, callback) {
}


// 批量上传文件
function uploadFiles(params, callback) {
var self = this;

// 判断多大的文件使用分片上传
var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize;

// 汇总返回进度
var TotalSize = 0;
var TotalFinish = 0;
var onTotalProgress = util.throttleOnProgress.call(self, TotalFinish, params.onProgress);

// 汇总返回回调
var unFinishCount = params.files.length;
var _onTotalFileFinish = params.onFileFinish;
var resultList = Array(unFinishCount);
var onTotalFileFinish = function (err, data, options) {
onTotalProgress(null, true);
_onTotalFileFinish && _onTotalFileFinish(err, data, options);
resultList[options.Index] = {
options: options,
error: err,
data: data
};
if (--unFinishCount <= 0 && callback) {
callback(null, {
files: resultList,
});
}
};

// 开始处理每个文件
var taskList = [];
util.each(params.files, function (fileParams, index) {

var Body = fileParams.Body;
var FileSize = Body.size || Body.length || 0;
var fileInfo = {Index: index, TaskId: ''};

// 更新文件总大小
TotalSize += FileSize;

// 整理 option,用于返回给回调
util.each(fileParams, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
});

// 处理单个文件 TaskReady
var _TaskReady = fileParams.TaskReady;
var TaskReady = function (tid) {
fileInfo.TaskId = tid;
_TaskReady && _TaskReady(tid);
};
fileParams.TaskReady = TaskReady;

// 处理单个文件进度
var PreAddSize = 0;
var _onProgress = fileParams.onProgress;
var onProgress = function (info) {
TotalFinish = TotalFinish - PreAddSize + info.loaded;
PreAddSize = info.loaded;
_onProgress && _onProgress(info);
onTotalProgress({loaded: TotalFinish, total: TotalSize});
};
fileParams.onProgress = onProgress;

// 处理单个文件完成
var _onFileFinish = fileParams.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data);
onTotalFileFinish && onTotalFileFinish(err, data, fileInfo);
};

// 添加上传任务
taskList.push({
api: FileSize >= SliceSize ? 'sliceUploadFile' : 'putObject',
params: fileParams,
callback: onFileFinish,
});
});
self._addTasks(taskList);
}


var API_MAP = {
sliceUploadFile: sliceUploadFile,
abortUploadTask: abortUploadTask,
uploadFiles: uploadFiles,
};

util.each(API_MAP, function (fn, apiName) {
Expand Down
Loading

0 comments on commit 8d12e9b

Please sign in to comment.