Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
1.demo新增上传base64文件内容
2.sliceUploadFile在onProgress里返回UploadId
  • Loading branch information
livehigh committed Jun 3, 2021
1 parent 4a04539 commit 1e2ca3a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
42 changes: 42 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,46 @@ function putObject() {
});
}

// 简单上传 文件boby为base64
function putObject_base64ToBlob() {
var base64Url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAABRFBMVEUAAAAAo/8Ao/8Ao/8Ao/8ApP8Aov8Ao/8Abv8Abv8AyNwAyNwAo/8Ao/8Ao/8Abv8Ao/8AivgAo/8AyNwAbv8Abv8AydwApf8Abf8Ao/8AbP8Ao/8AyNwAydwAbv8AydwApP8Ao/8AyNwAo/8AyNwAydsAyNwAxd8Aov8AyNwAytsAo/8Abv8AyNwAbv8Av+MAo/8AytsAo/8Abv8AyNwAo/8Abv8AqfkAbv8Aov8Abv8AyNwAov8Abv8Ao/8Abv8Ao/8AydwAo/8Ao/8Ate8Ay9oAvOcAof8AveAAyNwAyNwAo/8AyNwAy9kAo/8AyNwAyNwAo/8AqP8Aaf8AyNwAbv0Abv8Abv8AaP8Ao/8Ao/8Ao/8Ao/8Abv8AyNwAgvcAaP8A0dkAo/8AyNwAav8Abv8Ao/8Abv8AyNwAy9sAvOUAtePdkYxjAAAAZnRSTlMAw/co8uAuJAn8+/Tt29R8DAX77+nZz87Jv6CTh3lxTklAPjouJRsL5tjAuLiyr62roaCakYp0XVtOQTMyLiohICAcGRP49vTv5+PJurawq6mnnJuYl4+OiIB7eXVvX15QSDgqHxNcw3l6AAABe0lEQVQ4y82P11oCQQxGIy5FUJpKk6aAhV6k92LvvXedDfj+92ZkYQHxnnMxu3/OfJMEJo6y++baXf5XVw22GVGcsRmq431mQZRYyIzRGgdXi+HwIv86NDBKisrRAtU1hSj9pkZ9jpo/9YKbRsmNNKCHDXI00BxfMMirKNpMcjQ5Lm4/YZArUXyBYUwg40nsdr5jb3LBe25VWpNeKa1GENsEnq52C80z1uW48estiKjb19G54QdCrScnKAU69U3KJ4jzrsBawDWPuOcBqMyRvlcb1Y+zjMUBVsivAKe4gXgEKiVjSh9wlunGMmwiOqFL3RI0cj+nkgp3jC1BELVFkGiZSuvkp3tZZWZ2sKCuDj185PXqfmwI7AAOUctHkJoOeXg3sxA4ES+l7CVvrYHMEmNp8GtR+wycPG0+1RrwWQUzl4CvgQmPP5Ddofl8tWkJVT7J+BIAaxEktrYZoRAUfXgOGYHfcOqw3WF/EdLccz5cMfvUCPb4QwUmhB8+v12HZPCkbgAAAABJRU5ErkJggg==';
var dataURLtoBlob = function (dataurl) {
var arr = dataurl.split(',');
var mime = arr[0].match(/:(.*?);/)[1];
var bstr = atob(arr[1]);
var n = bstr.length;
var u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
};
// 调用方法
cos.putObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: 'base64_file.png', /* 必须 */
Body: dataURLtoBlob(base64Url),
onTaskReady: function (tid) {
TaskId = tid;
logger.log('onTaskReady', tid);
},
onTaskStart: function (info) {
logger.log('onTaskStart', info);
},
onProgress: function (progressData) {
logger.log(JSON.stringify(progressData));
},
Headers: {
// 万象持久化接口,上传时持久化
// 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "test.jpg", "rule": "imageMogr2/thumbnail/!50p"}]}'
'content-type': 'image/png',
},
}, function (err, data) {
logger.log('putObject:', err || data);
});
}

function putObjectCopy() {
cos.putObjectCopy({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Expand Down Expand Up @@ -1647,6 +1687,7 @@ function CIExample4(){
'abortUploadTask',
'selectObjectContent',
'putObject',
'putObject_base64ToBlob',

'header-高级操作',
'sliceUploadFile',
Expand All @@ -1671,6 +1712,7 @@ function CIExample4(){
];
var labelMap = {
putObject: '简单上传',
putObject_base64ToBlob: '简单上传:base64转blob',
sliceUploadFile: '分片上传',
sliceCopyFile: '分片复制',
uploadFiles: '批量上传文件',
Expand Down
12 changes: 8 additions & 4 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ var apiWrapper = function (apiName, apiFn) {
};
};

var throttleOnProgress = function (total, onProgress) {
var throttleOnProgress = function (total, onProgress, uploadId) {
var self = this;
var size0 = 0;
var size1 = 0;
Expand All @@ -651,7 +651,11 @@ var throttleOnProgress = function (total, onProgress) {
time0 = time1;
size0 = size1;
try {
onProgress({ loaded: size1, total: total, speed: speed, percent: percent });
var info = { loaded: size1, total: total, speed: speed, percent: percent };
if (uploadId) {
info.uploadId = uploadId;
}
onProgress(info);
} catch (e) {}
}
}
Expand Down Expand Up @@ -8632,6 +8636,8 @@ function sliceUploadFile(params, callback) {
uuid && session.saveUploadId.call(self, uuid, UploadData.UploadId, self.options.UploadIdCacheLimit); // 缓存 UploadId
session.setUsing(UploadData.UploadId); // 标记 UploadId 为正在使用

onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress, UploadData.UploadId);

// 获取 UploadId
onProgress(null, true); // 任务状态开始 uploading
uploadSliceList.call(self, {
Expand Down Expand Up @@ -8659,8 +8665,6 @@ function sliceUploadFile(params, callback) {
// 开始获取文件 UploadId,里面会视情况计算 ETag,并比对,保证文件一致性,也优化上传
ep.on('get_file_size_finish', function () {

onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);

if (params.UploadData.UploadId) {
ep.emit('get_upload_data_finish', params.UploadData);
} else {
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function sliceUploadFile(params, callback) {
uuid && session.saveUploadId.call(self, uuid, UploadData.UploadId, self.options.UploadIdCacheLimit); // 缓存 UploadId
session.setUsing(UploadData.UploadId); // 标记 UploadId 为正在使用

onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress, UploadData.UploadId);

// 获取 UploadId
onProgress(null, true); // 任务状态开始 uploading
uploadSliceList.call(self, {
Expand Down Expand Up @@ -94,8 +96,6 @@ function sliceUploadFile(params, callback) {
// 开始获取文件 UploadId,里面会视情况计算 ETag,并比对,保证文件一致性,也优化上传
ep.on('get_file_size_finish', function () {

onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);

if (params.UploadData.UploadId) {
ep.emit('get_upload_data_finish', params.UploadData);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.2.10';
COS.version = '1.2.11';

module.exports = COS;
10 changes: 7 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ var apiWrapper = function (apiName, apiFn) {
}
};

var throttleOnProgress = function (total, onProgress) {
var throttleOnProgress = function (total, onProgress, uploadId) {
var self = this;
var size0 = 0;
var size1 = 0;
Expand All @@ -589,7 +589,11 @@ var throttleOnProgress = function (total, onProgress) {
time0 = time1;
size0 = size1;
try {
onProgress({loaded: size1, total: total, speed: speed, percent: percent});
var info = { loaded: size1, total: total, speed: speed, percent: percent };
if (uploadId) {
info.uploadId = uploadId;
}
onProgress(info);
} catch (e) {
}
}
Expand All @@ -613,7 +617,7 @@ var throttleOnProgress = function (total, onProgress) {
var getFileSize = function (api, params, callback) {
var size;
if (typeof params.Body === 'string') {
params.Body = new Blob([params.Body], {type: 'text/plain'});
params.Body = new Blob([params.Body], {type: 'text/plain'});
} else if (params.Body instanceof ArrayBuffer) {
params.Body = new Blob([params.Body]);
}
Expand Down

0 comments on commit 1e2ca3a

Please sign in to comment.