Skip to content

Commit

Permalink
Merge pull request #107 from livehigh/feat/add-uploadFile
Browse files Browse the repository at this point in the history
feat:新增高级上传接口
  • Loading branch information
carsonxu authored Jun 11, 2021
2 parents 091cc91 + 802e644 commit 2e235b3
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 deletions.
26 changes: 26 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,31 @@ function restartTask() {
console.log('restart');
}

function uploadFile() {
var filename = '3mb.zip';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024 * 3, function (err) {
cos.uploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: filename,
FilePath: filepath,
SliceSize: 1024 * 1024 * 5, // 大于5mb才进行分块上传
onProgress: function (info) {
var percent = Math.floor(info.percent * 10000) / 100;
var speed = Math.floor(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);
fs.unlinkSync(filepath);
});
});
}

function uploadFiles() {
var filepath = path.resolve(__dirname, '1mb.zip');
util.createFile(filepath, 1024 * 1024 * 10, function (err) {
Expand Down Expand Up @@ -1682,6 +1707,7 @@ function CIExample4(){
// selectObjectContentStream();
// selectObjectContent();
// sliceUploadFile();
// uploadFile();
// uploadFiles();
// cancelTask();
// pauseTask();
Expand Down
35 changes: 34 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,35 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */
}[],
}

// uploadFile 高级上传
interface FileFinishInfo {
err: Error;
data: UploadFileItemResult;
options: UploadFileItemParams;
}
type onFileFinish = (params: FileFinishInfo) => void;

type UploadFileParams = (PutObjectParams | SliceUploadFileParams) & {
/** 要上传的本地文件路径 */
FilePath: string,
/** 使用 uploadFile 高级上传时,文件大小大于该数值将使用按分块上传,否则将调用简单上传,单位 Byte,默认值1048576(1MB) */
SliceSize?: number,
/** 上传的进度回调方法 */
onProgress?: onProgress,
/** 上传完成回调方法 */
onFileFinish?: onFileFinish,
};

/** uploadFiles 接口返回值 */
interface UploadFileResult extends GeneralResult {
/** 对象的实体标签(Entity Tag),是对象被创建时标识对象内容的信息标签,可用于检查对象的内容是否发生变化,例如"8e0b617ca298a564c3331da28dcb50df"。此头部并不一定返回对象的 MD5 值,而是根据对象上传和加密方式而有所不同 */
ETag: ETag,
/** 创建的存储桶访问地址,不带 https:// 前缀,例如 examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/images/1.jpg */
Location: string,
/** 对象的版本 ID */
VersionId?: VersionId,
}

// sliceCopyFile
/** sliceCopyFile 接口参数 */
interface SliceCopyFileParams extends ObjectParams {
Expand Down Expand Up @@ -2169,7 +2198,11 @@ declare class COS {
abortUploadTask(params: COS.AbortUploadTaskParams, callback: (err: COS.CosError, data: COS.AbortUploadTaskResult) => void): void;
abortUploadTask(params: COS.AbortUploadTaskParams): Promise<COS.AbortUploadTaskResult>;

/** 分片复制文件 */
/** 高级上传文件 */
uploadFile(params: COS.UploadFileParams, callback: (err: COS.CosError, data: COS.UploadFileResult) => void): void;
uploadFile(params: COS.UploadFileParams): Promise<COS.UploadFileResult>;

/** 批量上传文件 */
uploadFiles(params: COS.UploadFilesParams, callback: (err: COS.CosError, data: COS.UploadFilesResult) => void): void;
uploadFiles(params: COS.UploadFilesParams): Promise<COS.UploadFilesResult>;

Expand Down
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.9.18",
"version": "2.9.19",
"description": "cos nodejs sdk v5",
"main": "index.js",
"types": "index.d.ts",
Expand Down
53 changes: 53 additions & 0 deletions sdk/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,58 @@ function abortUploadTaskArray(params, callback) {
});
}

// 高级上传
function uploadFile(params, callback) {
var self = this;

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

// 开始处理每个文件
var taskList = [];

fs.stat(params.FilePath, function (err, stat) {

var isDir = stat.isDirectory();
var FileSize = params.ContentLength = stat.size || 0;
var fileInfo = {TaskId: ''};

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

// 处理文件 TaskReady
var _onTaskReady = params.onTaskReady;
var onTaskReady = function (tid) {
fileInfo.TaskId = tid;
_onTaskReady && _onTaskReady(tid);
};
params.onTaskReady = onTaskReady;

// 处理文件完成
var _onFileFinish = params.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data, fileInfo);
callback && callback(err, data);
};

// 添加上传任务
var api = FileSize <= SliceSize || isDir ? 'putObject' : 'sliceUploadFile';
if (api === 'putObject') {
params.Body = isDir ? '' : fs.createReadStream(params.FilePath);
params.Body.isSdkCreated = true;
}
taskList.push({
api: api,
params: params,
callback: onFileFinish,
});
self._addTasks(taskList);
});
}

// 批量上传文件
function uploadFiles(params, callback) {
Expand Down Expand Up @@ -1352,6 +1404,7 @@ function downloadFile(params, callback) {
var API_MAP = {
sliceUploadFile: sliceUploadFile,
abortUploadTask: abortUploadTask,
uploadFile: uploadFile,
uploadFiles: uploadFiles,
sliceCopyFile: sliceCopyFile,
downloadFile: downloadFile,
Expand Down
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3613,6 +3613,58 @@ group('restoreObject()', function () {
});
});

group('uploadFile()', function () {
test('uploadFile() 高级上传', function (done, assert) {
var filename = '3mb.zip';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024 * 3, function (err) {
cos.uploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: filename,
FilePath: filepath,
}, function (err, data) {
assert.ok(!err);
fs.unlinkSync(filepath);
done();
});
});
});
test('uploadFile() 高级上传目录', function (done, assert) {
var filename = '3mb/';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024 * 3, function (err) {
cos.uploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: filename,
FilePath: filepath,
}, function (err, data) {
assert.ok(!err);
fs.unlinkSync(filepath);
done();
});
});
});
test('uploadFile() 高级上传 大于5mb则分块上传', function (done, assert) {
var filename = '3mb.zip';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024 * 3, function (err) {
cos.uploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: filename,
FilePath: filepath,
SliceSize: 1024 * 1024 * 5,
}, function (err, data) {
assert.ok(!err);
fs.unlinkSync(filepath);
done();
});
});
});
});

group('uploadFiles()', function () {
test('uploadFiles()', function (done, assert) {
var filename = '1.zip';
Expand Down

0 comments on commit 2e235b3

Please sign in to comment.