Skip to content

Commit 6ffee7f

Browse files
committed
修复取消成功的上传报错,签名参数增加 Pathname
1 parent 06304b9 commit 6ffee7f

File tree

6 files changed

+72
-60
lines changed

6 files changed

+72
-60
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cos-nodejs-sdk-v5",
3-
"version": "2.5.5",
3+
"version": "2.5.6",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"scripts": {

sdk/advance.js

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,20 @@ function getUploadIdAndPartList(params, callback) {
260260
Size: ChunkSize
261261
});
262262
} else {
263-
var chunkItem = util.fileSlice(params.FilePath, start, end);
264-
util.getFileMd5(chunkItem, function (err, md5) {
265-
if (err) return callback(err);
266-
var ETag = '"' + md5 + '"';
267-
ETagMap[PartNumber] = ETag;
268-
FinishSliceCount += 1;
269-
FinishSize += ChunkSize;
270-
callback(err, {
271-
PartNumber: PartNumber,
272-
ETag: ETag,
273-
Size: ChunkSize
263+
util.fileSlice(params.FilePath, start, end, function (chunkItem) {
264+
util.getFileMd5(chunkItem, function (err, md5) {
265+
if (err) return callback(err);
266+
var ETag = '"' + md5 + '"';
267+
ETagMap[PartNumber] = ETag;
268+
FinishSliceCount += 1;
269+
FinishSize += ChunkSize;
270+
callback(err, {
271+
PartNumber: PartNumber,
272+
ETag: ETag,
273+
Size: ChunkSize
274+
});
275+
onHashProgress({loaded: FinishSize, total: FileSize});
274276
});
275-
onHashProgress({loaded: FinishSize, total: FileSize});
276277
});
277278
}
278279
};
@@ -657,37 +658,39 @@ function uploadSliceItem(params, callback) {
657658
ContentLength = end - start;
658659
}
659660

660-
var md5Body = util.fileSlice(FilePath, start, end);
661-
util.getFileMd5(md5Body, function (err, md5) {
662-
var contentMd5 = md5 ? util.binaryBase64(md5) : '';
663-
var PartItem = UploadData.PartList[PartNumber - 1];
664-
Async.retry(ChunkRetryTimes, function (tryCallback) {
665-
if (!self._isRunningTask(TaskId)) return;
666-
var Body = util.fileSlice(FilePath, start, end);
667-
self.multipartUpload({
668-
TaskId: TaskId,
669-
Bucket: Bucket,
670-
Region: Region,
671-
Key: Key,
672-
ContentLength: ContentLength,
673-
PartNumber: PartNumber,
674-
UploadId: UploadData.UploadId,
675-
ServerSideEncryption: ServerSideEncryption,
676-
Body: Body,
677-
onProgress: params.onProgress,
678-
ContentMD5: contentMd5,
661+
util.fileSlice(FilePath, start, end, function (md5Body) {
662+
util.getFileMd5(md5Body, function (err, md5) {
663+
var contentMd5 = md5 ? util.binaryBase64(md5) : '';
664+
var PartItem = UploadData.PartList[PartNumber - 1];
665+
Async.retry(ChunkRetryTimes, function (tryCallback) {
666+
if (!self._isRunningTask(TaskId)) return;
667+
util.fileSlice(FilePath, start, end, function (Body) {
668+
self.multipartUpload({
669+
TaskId: TaskId,
670+
Bucket: Bucket,
671+
Region: Region,
672+
Key: Key,
673+
ContentLength: ContentLength,
674+
PartNumber: PartNumber,
675+
UploadId: UploadData.UploadId,
676+
ServerSideEncryption: ServerSideEncryption,
677+
Body: Body,
678+
onProgress: params.onProgress,
679+
ContentMD5: contentMd5,
680+
}, function (err, data) {
681+
if (!self._isRunningTask(TaskId)) return;
682+
if (err) {
683+
return tryCallback(err);
684+
} else {
685+
PartItem.Uploaded = true;
686+
return tryCallback(null, data);
687+
}
688+
});
689+
});
679690
}, function (err, data) {
680691
if (!self._isRunningTask(TaskId)) return;
681-
if (err) {
682-
return tryCallback(err);
683-
} else {
684-
PartItem.Uploaded = true;
685-
return tryCallback(null, data);
686-
}
692+
return callback(err, data);
687693
});
688-
}, function (err, data) {
689-
if (!self._isRunningTask(TaskId)) return;
690-
return callback(err, data);
691694
});
692695
});
693696
}
@@ -973,7 +976,7 @@ function sliceCopyFile(params, callback) {
973976
var CopySliceSize = params.SliceSize === undefined ? self.options.CopySliceSize : params.SliceSize;
974977
CopySliceSize = Math.max(0, Math.min(CopySliceSize, 5 * 1024 * 1024 * 1024));
975978

976-
var ChunkSize = params.ChunkSize || this.options.ChunkSize;
979+
var ChunkSize = params.ChunkSize || this.options.CopyChunkSize;
977980
var ChunkParallel = this.options.CopyChunkParallelLimit;
978981

979982
var FinishSize = 0;

sdk/base.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,10 +2138,11 @@ function getAuthorizationAsync(params, callback) {
21382138
var Region = params.Region || '';
21392139

21402140
// PathName
2141-
var PathName = params.Key || '';
2141+
var KeyName = params.Key || '';
21422142
if (self.options.ForcePathStyle && Bucket) {
2143-
PathName = Bucket + '/' + PathName;
2143+
KeyName = Bucket + '/' + KeyName;
21442144
}
2145+
var Pathname = '/' + KeyName;
21452146

21462147
// Action、ResourceKey
21472148
var StsData = {};
@@ -2164,7 +2165,8 @@ function getAuthorizationAsync(params, callback) {
21642165
var i, AuthData;
21652166
for (i = self._StsCache.length - 1; i >= 0; i--) {
21662167
AuthData = self._StsCache[i];
2167-
if (AuthData.ExpiredTime < Math.round(util.getSkewTime(self.options.SystemClockOffset) / 1000) + 30) {
2168+
var compareTime = Math.round(util.getSkewTime(self.options.SystemClockOffset) / 1000) + 30;
2169+
if (AuthData.StartTime && compareTime < AuthData.StartTime || compareTime >= AuthData.ExpiredTime) {
21682170
self._StsCache.splice(i, 1);
21692171
continue;
21702172
}
@@ -2180,7 +2182,7 @@ function getAuthorizationAsync(params, callback) {
21802182
SecretId: StsData.TmpSecretId,
21812183
SecretKey: StsData.TmpSecretKey,
21822184
Method: params.Method,
2183-
Key: PathName,
2185+
Pathname: Pathname,
21842186
Query: params.Query,
21852187
Headers: params.Headers,
21862188
UseRawKey: self.options.UseRawKey,
@@ -2204,7 +2206,8 @@ function getAuthorizationAsync(params, callback) {
22042206
Bucket: Bucket,
22052207
Region: Region,
22062208
Method: params.Method,
2207-
Key: PathName,
2209+
Key: KeyName,
2210+
Pathname: Pathname,
22082211
Query: params.Query,
22092212
Headers: params.Headers,
22102213
Scope: Scope,
@@ -2244,7 +2247,7 @@ function getAuthorizationAsync(params, callback) {
22442247
SecretId: params.SecretId || self.options.SecretId,
22452248
SecretKey: params.SecretKey || self.options.SecretKey,
22462249
Method: params.Method,
2247-
Key: PathName,
2250+
Pathname: Pathname,
22482251
Query: params.Query,
22492252
Headers: params.Headers,
22502253
Expires: params.Expires,

sdk/cos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var defaultOptions = {
1111
AppId: '', // AppId 已废弃,请拼接到 Bucket 后传入,例如:test-1250000000
1212
SecretId: '',
1313
SecretKey: '',
14-
XCosSecurityToken: '',
14+
XCosSecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
1515
FileParallelLimit: 3,
1616
ChunkParallelLimit: 3,
1717
ChunkRetryTimes: 3,

sdk/task.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ var initTask = function (cos) {
7979
startNextTask(cos);
8080
task.callback && task.callback(err, data);
8181
if (task.state === 'success') {
82-
delete task.params.UploadData;
83-
delete task.params.Body;
84-
delete task.params;
82+
if (task.params) {
83+
delete task.params.UploadData;
84+
delete task.params.Body;
85+
delete task.params;
86+
}
8587
delete task.callback;
8688
}
8789
}
@@ -114,12 +116,15 @@ var initTask = function (cos) {
114116
startNextTask(cos);
115117
}
116118
if (switchToState === 'canceled') {
117-
delete task.params.UploadData;
118-
delete task.params.Body;
119-
delete task.params;
119+
if (task.params) {
120+
delete task.params.UploadData;
121+
delete task.params.Body;
122+
delete task.params;
123+
}
120124
delete task.callback;
121125
}
122126
}
127+
clearQueue();
123128
};
124129

125130
cos._addTasks = function (taskList) {

sdk/util.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ var getAuth = function (opt) {
3030
var Key = opt.Key || '';
3131
var pathname;
3232
if (opt.UseRawKey) {
33-
pathname = opt.pathname || '/' + Key;
33+
pathname = opt.Pathname || opt.pathname || '/' + Key;
3434
} else {
35-
pathname = opt.pathname || Key;
35+
pathname = opt.Pathname || opt.pathname || Key;
3636
pathname.indexOf('/') !== 0 && (pathname = '/' + pathname);
3737
}
3838

@@ -571,11 +571,12 @@ var util = {
571571
removeItem: update,
572572
};
573573
})();
574-
util.fileSlice = function (FilePath, start, end) {
574+
util.fileSlice = function (FilePath, start, end, callback) {
575575
if (FilePath) {
576-
return fs.createReadStream(FilePath, {start: start, end: end - 1});
576+
callback(fs.createReadStream(FilePath, {start: start, end: end - 1}));
577+
} else {
578+
callback(null);
577579
}
578-
return null;
579580
};
580581
util.getFileUUID = function (FileStat, ChunkSize) {
581582
if (FileStat && FileStat.FilePath && FileStat.size && FileStat.ctime && FileStat.mtime && ChunkSize) {

0 commit comments

Comments
 (0)