diff --git a/demo-album/lib/cos-wx-sdk-v5.js b/demo-album/lib/cos-wx-sdk-v5.js index bb36b64..dfbab8b 100644 --- a/demo-album/lib/cos-wx-sdk-v5.js +++ b/demo-album/lib/cos-wx-sdk-v5.js @@ -2338,7 +2338,7 @@ base.init(COS, task); advance.init(COS, task); COS.getAuthorization = util.getAuth; -COS.version = '1.0.8'; +COS.version = '1.0.9'; module.exports = COS; @@ -9884,6 +9884,48 @@ function abortUploadTaskArray(params, callback) { }); } +// 高级上传 +function uploadFile(params, callback) { + var self = this; + + // 判断多大的文件使用分片上传 + var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; + + var taskList = []; + + var FileSize = params.FileSize; + 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; + params.onTaskReady = function (tid) { + fileInfo.TaskId = tid; + _onTaskReady && _onTaskReady(tid); + }; + + // 处理文件完成 + var _onFileFinish = params.onFileFinish; + var onFileFinish = function (err, data) { + _onFileFinish && _onFileFinish(err, data, fileInfo); + callback && callback(err, data); + }; + + // 添加上传任务 + var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject'; + taskList.push({ + api: api, + params: params, + callback: onFileFinish, + }); + self._addTasks(taskList); +} // 批量上传文件 function uploadFiles(params, callback) { @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/demo/demo-sdk.js b/demo/demo-sdk.js index 3eeb6a7..134ec00 100644 --- a/demo/demo-sdk.js +++ b/demo/demo-sdk.js @@ -151,6 +151,67 @@ var dao = { console.log(err || data) }); }, + 'uploadFile 高级上传': function() { + var uploadFile = function(file) { + cos.uploadFile({ + Bucket: config.Bucket, + Region: config.Region, + Key: file.name, + FilePath: file.path, + SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传 + 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); + }); + } + wx.chooseMessageFile({ + count: 10, + type: 'all', + success: function(res) { + uploadFile(res.tempFiles[0]); + } + }); + }, + 'uploadFiles 批量上传': function() { + var uploadFiles = function(files) { + const fileList = files.map(function(file) { + return Object.assign(file, { + Bucket: config.Bucket, + Region: config.Region, + Key: file.name, + FilePath: file.path, + }); + }); + cos.uploadFiles({ + files: fileList, + SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传 + 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); + }); + } + wx.chooseMessageFile({ + count: 10, + type: 'all', + success: function(res) { + uploadFiles(res.tempFiles); + } + }); + }, '分片上传': function() { var sliceUploadFile = function (file) { var key = file.name; diff --git a/demo/lib/cos-wx-sdk-v5.js b/demo/lib/cos-wx-sdk-v5.js index bb36b64..dfbab8b 100644 --- a/demo/lib/cos-wx-sdk-v5.js +++ b/demo/lib/cos-wx-sdk-v5.js @@ -2338,7 +2338,7 @@ base.init(COS, task); advance.init(COS, task); COS.getAuthorization = util.getAuth; -COS.version = '1.0.8'; +COS.version = '1.0.9'; module.exports = COS; @@ -9884,6 +9884,48 @@ function abortUploadTaskArray(params, callback) { }); } +// 高级上传 +function uploadFile(params, callback) { + var self = this; + + // 判断多大的文件使用分片上传 + var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; + + var taskList = []; + + var FileSize = params.FileSize; + 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; + params.onTaskReady = function (tid) { + fileInfo.TaskId = tid; + _onTaskReady && _onTaskReady(tid); + }; + + // 处理文件完成 + var _onFileFinish = params.onFileFinish; + var onFileFinish = function (err, data) { + _onFileFinish && _onFileFinish(err, data, fileInfo); + callback && callback(err, data); + }; + + // 添加上传任务 + var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject'; + taskList.push({ + api: api, + params: params, + callback: onFileFinish, + }); + self._addTasks(taskList); +} // 批量上传文件 function uploadFiles(params, callback) { @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/package.json b/package.json index b059284..04f6119 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cos-wx-sdk-v5", - "version": "1.0.8", + "version": "1.0.9", "description": "小程序 SDK for [腾讯云对象存储服务](https://cloud.tencent.com/product/cos)", "main": "demo/lib/cos-wx-sdk-v5.js", "scripts": { diff --git a/src/advance.js b/src/advance.js index 7df61cd..090a730 100644 --- a/src/advance.js +++ b/src/advance.js @@ -802,6 +802,48 @@ function abortUploadTaskArray(params, callback) { }); } +// 高级上传 +function uploadFile(params, callback) { + var self = this; + + // 判断多大的文件使用分片上传 + var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; + + var taskList = []; + + var FileSize = params.FileSize; + 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; + params.onTaskReady = function (tid) { + fileInfo.TaskId = tid; + _onTaskReady && _onTaskReady(tid); + }; + + // 处理文件完成 + var _onFileFinish = params.onFileFinish; + var onFileFinish = function (err, data) { + _onFileFinish && _onFileFinish(err, data, fileInfo); + callback && callback(err, data); + }; + + // 添加上传任务 + var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject'; + taskList.push({ + api: api, + params: params, + callback: onFileFinish, + }); + self._addTasks(taskList); +} // 批量上传文件 function uploadFiles(params, callback) { @@ -1135,6 +1177,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/src/cos.js b/src/cos.js index bf8180f..80f4c01 100644 --- a/src/cos.js +++ b/src/cos.js @@ -57,6 +57,6 @@ base.init(COS, task); advance.init(COS, task); COS.getAuthorization = util.getAuth; -COS.version = '1.0.8'; +COS.version = '1.0.9'; module.exports = COS;