diff --git a/demo/demo.js b/demo/demo.js index 0c8875e..0330097 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -822,6 +822,40 @@ function putObject() { }); } +function putObject_base64ToBuffer() { + // 创建测试文件 + var filename = 'test.png'; + var filepath = path.resolve(__dirname, filename); + 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 body = Buffer.from(base64Url.split(',')[1] , 'base64'); + util.createFile(filepath, 1024 * 1024, function (err) { + // 调用方法 + cos.putObject({ + Bucket: config.Bucket, /* 必须 */ + Region: config.Region, + Key: filename, /* 必须 */ + onTaskReady: function (tid) { + TaskId = tid; + }, + onProgress: function (progressData) { + console.log(JSON.stringify(progressData)); + }, + // 格式1. 传入文件内容 + // Body: fs.readFileSync(filepath), + // 格式2. 传入文件流,必须需要传文件大小 + Body: body, + ContentLength: body.length, + Headers: { + // 万象持久化接口,上传时持久化 + // 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "test.jpg", "rule": "imageMogr2/thumbnail/!50p"}]}' + }, + }, function (err, data) { + console.log(err || data); + fs.unlinkSync(filepath); + }); +}); +} + function putObjectCopy() { cos.putObjectCopy({ Bucket: config.Bucket, @@ -1653,6 +1687,7 @@ function CIExample4(){ // pauseTask(); // restartTask(); // putObject(); +// putObject_base64(); // sliceCopyFile(); // putObjectTagging(); // getObjectTagging(); diff --git a/package.json b/package.json index de79367..748249e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cos-nodejs-sdk-v5", - "version": "2.9.17", + "version": "2.9.18", "description": "cos nodejs sdk v5", "main": "index.js", "types": "index.d.ts", diff --git a/sdk/advance.js b/sdk/advance.js index 8a4103b..364d0bb 100644 --- a/sdk/advance.js +++ b/sdk/advance.js @@ -25,12 +25,18 @@ function sliceUploadFile(params, callback) { // 上传过程中出现错误,返回错误 ep.on('error', function (err) { if (!self._isRunningTask(TaskId)) return; - return callback(err); + var _err = util.extend({ + UploadId: params.UploadData.UploadId || '' + }, err); + return callback(_err); }); // 上传分块完成,开始 uploadSliceComplete 操作 ep.on('upload_complete', function (UploadCompleteData) { - callback(null, UploadCompleteData); + var _UploadCompleteData = util.extend({ + UploadId: params.UploadData.UploadId || '' + }, UploadCompleteData); + callback(null, _UploadCompleteData); }); // 上传分块完成,开始 uploadSliceComplete 操作