diff --git a/index.d.ts b/index.d.ts index 8cd403d..0b440a4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -127,6 +127,8 @@ declare namespace COS { /** 上传/下载的进度回调方法 */ type onProgress = (params: ProgressInfo) => any; + type CopySourceParserFunction = (source: string) => null | { Bucket: string; Region: string; Key: string }; + // 实例参数 interface COSOptions { /** 固定密钥的 SecretId,可从{@link https://console.cloud.tencent.com/cam/capi|API密钥管理}获取 */ @@ -199,6 +201,8 @@ declare namespace COS { /** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */ ForceSignHost?: boolean; AutoSwitchHost?: boolean; + /** 自定义拷贝源解析器 */ + CopySourceParser?: null | CopySourceParserFunction; /** 获取签名的回调方法,如果没有 SecretId、SecretKey 时,必选 */ getAuthorization?: ( options: GetAuthorizationOptions, @@ -1808,7 +1812,9 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */ // uploadFiles type UploadFileItemParams = (PutObjectParams | SliceUploadFileParams) & { /** 要上传的本地文件路径 */ - FilePath: string; + FilePath?: string; + /** 要上传对象内容 */ + Body?: UploadBody; /** 上传的进度回调方法 */ onProgress?: onProgress; /** 上传完成回调方法 */ diff --git a/package.json b/package.json index 39e161e..5a423b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cos-nodejs-sdk-v5", - "version": "2.13.1", + "version": "2.13.2", "description": "cos nodejs sdk v5", "main": "index.js", "types": "index.d.ts", diff --git a/sdk/advance.js b/sdk/advance.js index bc12ec6..5895fe9 100644 --- a/sdk/advance.js +++ b/sdk/advance.js @@ -971,60 +971,70 @@ function uploadFiles(params, callback) { var taskList = []; var count = params.files.length; util.each(params.files, function (fileParams, index) { - fs.stat(fileParams.FilePath, function (err, stat) { - var isDir = stat ? stat.isDirectory() : false; - var FileSize = (fileParams.ContentLength = stat ? stat.size : 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; - } + var isDir = false; + var FileSize = 0; + if (fileParams.Body) { + util.getFileSize('putObject', fileParams, function (err, size) { + FileSize = fileParams.ContentLengt = size; }); + } else if (fileParams.FilePath) { + var stat; + try { + stat = fs.statSync(fileParams.FilePath); + } catch (e) {} + isDir = stat ? stat.isDirectory() : false; + FileSize = fileParams.ContentLength = stat ? stat.size : 0; + } + var fileInfo = { Index: index, TaskId: '' }; - // 处理单个文件 TaskReady - var _onTaskReady = fileParams.onTaskReady; - var onTaskReady = function (tid) { - fileInfo.TaskId = tid; - _onTaskReady && _onTaskReady(tid); - }; - fileParams.onTaskReady = onTaskReady; - - // 处理单个文件进度 - 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); - }; + // 更新文件总大小 + TotalSize += FileSize; - // 添加上传任务 - var api = FileSize <= SliceSize || isDir ? 'putObject' : 'sliceUploadFile'; - if (api === 'putObject') { - fileParams.Body = isDir ? '' : fs.createReadStream(fileParams.FilePath); - fileParams.Body.isSdkCreated = true; + // 整理 option,用于返回给回调 + util.each(fileParams, function (v, k) { + if (typeof v !== 'object' && typeof v !== 'function') { + fileInfo[k] = v; } - taskList.push({ - api: api, - params: fileParams, - callback: onFileFinish, - }); - --count === 0 && self._addTasks(taskList); }); + + // 处理单个文件 TaskReady + var _onTaskReady = fileParams.onTaskReady; + var onTaskReady = function (tid) { + fileInfo.TaskId = tid; + _onTaskReady && _onTaskReady(tid); + }; + fileParams.onTaskReady = onTaskReady; + + // 处理单个文件进度 + 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); + }; + + // 添加上传任务,传入 Body 则只支持简单上传 + var api = FileSize <= SliceSize || isDir || fileParams.Body ? 'putObject' : 'sliceUploadFile'; + if (api === 'putObject' && fileParams.FilePath && !fileParams.Body) { + fileParams.Body = isDir ? '' : fs.createReadStream(fileParams.FilePath); + fileParams.Body.isSdkCreated = true; + } + taskList.push({ + api: api, + params: fileParams, + callback: onFileFinish, + }); + --count === 0 && self._addTasks(taskList); }); } diff --git a/sdk/base.js b/sdk/base.js index 56c32de..6462f6b 100644 --- a/sdk/base.js +++ b/sdk/base.js @@ -3092,7 +3092,7 @@ function multipartComplete(params, callback) { protocol: self.options.Protocol, domain: self.options.Domain, bucket: params.Bucket, - region: params.Region, + region: !self.options.UseAccelerate ? params.Region : 'accelerate', object: params.Key, isLocation: true, }); diff --git a/sdk/cos.js b/sdk/cos.js index ea99d11..d9301aa 100644 --- a/sdk/cos.js +++ b/sdk/cos.js @@ -45,6 +45,7 @@ var defaultOptions = { ConfCwd: '', ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true AutoSwitchHost: true, + CopySourceParser: null, // 自定义拷贝源解析器 // 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey Credentials: { secretId: '',