Skip to content

Commit

Permalink
修复跨区域复制 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jul 3, 2018
1 parent 1405fbe commit b480f94
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
28 changes: 18 additions & 10 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ var apiWrapper = function (apiName, apiFn) {
return;
}
// 判断 region 格式
if (params.Region && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
_callback({ error: 'param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224' });
return;
if (!this.options.IgnoreRegionFormat && params.Region && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
console.warn('param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224');
}
// 判断 region 格式
if (params.Region && params.Region.indexOf('cos.') > -1) {
Expand Down Expand Up @@ -1893,7 +1892,8 @@ var defaultOptions = {
ServiceDomain: '',
SliceSize: 1024 * 1024 * 20,
Protocol: '',
ChunkRetryTimes: 3
ChunkRetryTimes: 3,
IgnoreRegionFormat: false
};

// 对外暴露的类
Expand Down Expand Up @@ -11167,7 +11167,15 @@ function sliceCopyFile(params, callback) {
var Region = params.Region;
var Key = params.Key;
var CopySource = params.CopySource;
var CopyFileName = CopySource.slice(CopySource.indexOf('/') + 1, CopySource.length);
var m = CopySource.match(/^([^.]+-\d+)\.cos\.([^.]+)\.myqcloud\.com\/(.+)$/);
if (!m) {
callback({ error: 'CopySource format error' });
return;
}

var SourceBucket = m[1];
var SourceRegion = m[2];
var SourceKey = m[3];
var SliceSize = Math.min(params.SliceSize, 5 * 1024 * 1024 * 1024);

var ChunkSize = params.ChunkSize || this.options.ChunkSize;
Expand Down Expand Up @@ -11281,23 +11289,23 @@ function sliceCopyFile(params, callback) {

// 获取远端复制源文件的大小
self.headObject({
Bucket: Bucket,
Region: Region,
Key: CopyFileName
Bucket: SourceBucket,
Region: SourceRegion,
Key: SourceKey
}, function (err, data) {
if (err) {
if (err.statusCode && err.statusCode === 404) {
return callback({ ErrorStatus: CopyFileName + ' Not Exist' });
return callback({ ErrorStatus: SourceKey + ' Not Exist' });
} else {
callback(err);
}
return;
}

FileSize = params.FileSize = data.headers['content-length'];

if (FileSize === undefined || !FileSize) {
callback({ error: 'get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.' });
return;
}

// 开始上传
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,15 @@ function sliceCopyFile(params, callback) {
var Region = params.Region;
var Key = params.Key;
var CopySource = params.CopySource;
var CopyFileName = CopySource.slice(CopySource.indexOf('/') + 1, CopySource.length);
var m = CopySource.match(/^([^.]+-\d+)\.cos\.([^.]+)\.myqcloud\.com\/(.+)$/);
if (!m) {
callback({error: 'CopySource format error'});
return;
}

var SourceBucket = m[1];
var SourceRegion = m[2];
var SourceKey = m[3];
var SliceSize = Math.min(params.SliceSize, 5 * 1024 * 1024 * 1024);

var ChunkSize = params.ChunkSize || this.options.ChunkSize;
Expand Down Expand Up @@ -1059,23 +1067,23 @@ function sliceCopyFile(params, callback) {

// 获取远端复制源文件的大小
self.headObject({
Bucket: Bucket,
Region: Region,
Key: CopyFileName,
Bucket: SourceBucket,
Region: SourceRegion,
Key: SourceKey,
},function(err, data) {
if (err) {
if (err.statusCode && err.statusCode === 404) {
return callback({ ErrorStatus: CopyFileName + ' Not Exist' });
return callback({ ErrorStatus: SourceKey + ' Not Exist' });
} else {
callback(err);
}
return;
}

FileSize = params.FileSize = data.headers['content-length'];

if (FileSize === undefined || !FileSize) {
callback({error: 'get Content-Length error, please add "Content-Length" to CORS ExposeHeader setting.'});
return;
}

// 开始上传
Expand Down
1 change: 1 addition & 0 deletions src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var defaultOptions = {
SliceSize: 1024 * 1024 * 20,
Protocol: '',
ChunkRetryTimes: 3,
IgnoreRegionFormat: false,
};

// 对外暴露的类
Expand Down
5 changes: 2 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ var apiWrapper = function (apiName, apiFn) {
return;
}
// 判断 region 格式
if (params.Region && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
_callback({error: 'param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224'});
return;
if (!this.options.IgnoreRegionFormat && params.Region && params.Region.indexOf('-') === -1 && params.Region !== 'yfb' && params.Region !== 'default') {
console.warn('param Region format error, find help here: https://cloud.tencent.com/document/product/436/6224');
}
// 判断 region 格式
if (params.Region && params.Region.indexOf('cos.') > -1) {
Expand Down

0 comments on commit b480f94

Please sign in to comment.