Skip to content

Commit 810ef03

Browse files
committed
上传支持 Content-MD5
1 parent cb6870f commit 810ef03

File tree

6 files changed

+199
-145
lines changed

6 files changed

+199
-145
lines changed

dist/cos-js-sdk-v5.js

+103-73
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
exports["COS"] = factory();
88
else
99
root["COS"] = factory();
10-
})(this, function() {
10+
})(typeof self !== 'undefined' ? self : this, function() {
1111
return /******/ (function(modules) { // webpackBootstrap
1212
/******/ // The module cache
1313
/******/ var installedModules = {};
@@ -582,6 +582,22 @@ util.getFileUUID = function (file, ChunkSize) {
582582
return null;
583583
}
584584
};
585+
util.getBodyMd5 = function (UploadCheckContentMd5, Body, callback) {
586+
callback = callback || noop;
587+
if (UploadCheckContentMd5) {
588+
if (typeof Body === 'string') {
589+
callback(util.md5(Body, true));
590+
} else if (Body instanceof global.Blob) {
591+
util.getFileMd5(Body, function (err, md5) {
592+
callback(md5);
593+
});
594+
} else {
595+
callback();
596+
}
597+
} else {
598+
callback();
599+
}
600+
};
585601

586602
module.exports = util;
587603
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(5)))
@@ -1909,12 +1925,14 @@ var defaultOptions = {
19091925
CopySliceSize: 1024 * 1024 * 10,
19101926
ProgressInterval: 1000,
19111927
UploadQueueSize: 10000,
1928+
UploadIdCacheLimit: 50,
1929+
UploadCheckContentMd5: false,
19121930
Domain: '',
19131931
ServiceDomain: '',
19141932
Protocol: '',
19151933
CompatibilityMode: false,
19161934
ForcePathStyle: false,
1917-
UploadIdCacheLimit: 50
1935+
XCosSecurityToken: ''
19181936
};
19191937

19201938
// 对外暴露的类
@@ -1938,7 +1956,7 @@ util.extend(COS.prototype, base);
19381956
util.extend(COS.prototype, advance);
19391957

19401958
COS.getAuthorization = util.getAuth;
1941-
COS.version = '0.4.21';
1959+
COS.version = '0.4.22';
19421960

19431961
module.exports = COS;
19441962

@@ -4825,39 +4843,42 @@ function putObject(params, callback) {
48254843
var FileSize = params.ContentLength;
48264844
var onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);
48274845

4828-
submitRequest.call(self, {
4829-
TaskId: params.TaskId,
4830-
method: 'PUT',
4831-
Bucket: params.Bucket,
4832-
Region: params.Region,
4833-
Key: params.Key,
4834-
headers: params.Headers,
4835-
body: params.Body,
4836-
onProgress: onProgress
4837-
}, function (err, data) {
4838-
if (err) {
4839-
onProgress(null, true);
4840-
return callback(err);
4841-
}
4842-
onProgress({ loaded: FileSize, total: FileSize }, true);
4843-
if (data && data.headers && data.headers['etag']) {
4844-
var url = getUrl({
4845-
ForcePathStyle: self.options.ForcePathStyle,
4846-
protocol: self.options.Protocol,
4847-
domain: self.options.Domain,
4848-
bucket: params.Bucket,
4849-
region: params.Region,
4850-
object: params.Key
4851-
});
4852-
url = url.substr(url.indexOf('://') + 3);
4853-
return callback(null, {
4854-
Location: url,
4855-
ETag: data.headers['etag'],
4856-
statusCode: data.statusCode,
4857-
headers: data.headers
4858-
});
4859-
}
4860-
callback(null, data);
4846+
util.getBodyMd5(self.options.UploadCheckContentMd5, params.Body, function (md5) {
4847+
md5 && (params.Headers['Content-MD5'] = util.binaryBase64(md5));
4848+
submitRequest.call(self, {
4849+
TaskId: params.TaskId,
4850+
method: 'PUT',
4851+
Bucket: params.Bucket,
4852+
Region: params.Region,
4853+
Key: params.Key,
4854+
headers: params.Headers,
4855+
body: params.Body,
4856+
onProgress: onProgress
4857+
}, function (err, data) {
4858+
if (err) {
4859+
onProgress(null, true);
4860+
return callback(err);
4861+
}
4862+
onProgress({ loaded: FileSize, total: FileSize }, true);
4863+
if (data && data.headers && data.headers['etag']) {
4864+
var url = getUrl({
4865+
ForcePathStyle: self.options.ForcePathStyle,
4866+
protocol: self.options.Protocol,
4867+
domain: self.options.Domain,
4868+
bucket: params.Bucket,
4869+
region: params.Region,
4870+
object: params.Key
4871+
});
4872+
url = url.substr(url.indexOf('://') + 3);
4873+
return callback(null, {
4874+
Location: url,
4875+
ETag: data.headers['etag'],
4876+
statusCode: data.statusCode,
4877+
headers: data.headers
4878+
});
4879+
}
4880+
callback(null, data);
4881+
});
48614882
});
48624883
}
48634884

@@ -5246,28 +5267,31 @@ function multipartUpload(params, callback) {
52465267

52475268
var self = this;
52485269
util.getFileSize('multipartUpload', params, function () {
5249-
submitRequest.call(self, {
5250-
TaskId: params.TaskId,
5251-
method: 'PUT',
5252-
Bucket: params.Bucket,
5253-
Region: params.Region,
5254-
Key: params.Key,
5255-
qs: {
5256-
partNumber: params['PartNumber'],
5257-
uploadId: params['UploadId']
5258-
},
5259-
headers: params.Headers,
5260-
onProgress: params.onProgress,
5261-
body: params.Body || null
5262-
}, function (err, data) {
5263-
if (err) {
5264-
return callback(err);
5265-
}
5266-
data['headers'] = data['headers'] || {};
5267-
callback(null, {
5268-
ETag: data['headers']['etag'] || '',
5269-
statusCode: data.statusCode,
5270-
headers: data.headers
5270+
util.getBodyMd5(self.options.UploadCheckContentMd5, params.Body, function (md5) {
5271+
md5 && (params.Headers['Content-MD5'] = util.binaryBase64(md5));
5272+
submitRequest.call(self, {
5273+
TaskId: params.TaskId,
5274+
method: 'PUT',
5275+
Bucket: params.Bucket,
5276+
Region: params.Region,
5277+
Key: params.Key,
5278+
qs: {
5279+
partNumber: params['PartNumber'],
5280+
uploadId: params['UploadId']
5281+
},
5282+
headers: params.Headers,
5283+
onProgress: params.onProgress,
5284+
body: params.Body || null
5285+
}, function (err, data) {
5286+
if (err) {
5287+
return callback(err);
5288+
}
5289+
data['headers'] = data['headers'] || {};
5290+
callback(null, {
5291+
ETag: data['headers']['etag'] || '',
5292+
statusCode: data.statusCode,
5293+
headers: data.headers
5294+
});
52715295
});
52725296
});
52735297
});
@@ -5529,7 +5553,7 @@ function getObjectUrl(params, callback) {
55295553
callback(null, { Url: url });
55305554
return url;
55315555
}
5532-
var authorization = getAuthorizationAsync.call(this, {
5556+
var AuthData = getAuthorizationAsync.call(this, {
55335557
Bucket: params.Bucket || '',
55345558
Region: params.Region || '',
55355559
Method: params.Method || 'get',
@@ -5538,7 +5562,7 @@ function getObjectUrl(params, callback) {
55385562
}, function (AuthData) {
55395563
if (!callback) return;
55405564
var signUrl = url;
5541-
signUrl += '?sign=' + encodeURIComponent(AuthData.Authorization);
5565+
signUrl += '?' + AuthData.Authorization;
55425566
AuthData.XCosSecurityToken && (signUrl += '&x-cos-security-token=' + AuthData.XCosSecurityToken);
55435567
AuthData.ClientIP && (signUrl += '&clientIP=' + AuthData.ClientIP);
55445568
AuthData.ClientUA && (signUrl += '&clientUA=' + AuthData.ClientUA);
@@ -5547,8 +5571,8 @@ function getObjectUrl(params, callback) {
55475571
callback(null, { Url: signUrl });
55485572
});
55495573
});
5550-
if (authorization) {
5551-
return url + '?sign=' + encodeURIComponent(authorization);
5574+
if (AuthData) {
5575+
return url + '?' + AuthData.Authorization + (AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '');
55525576
} else {
55535577
return url;
55545578
}
@@ -5730,17 +5754,23 @@ function getAuthorizationAsync(params, callback) {
57305754
});
57315755
} else {
57325756
// 内部计算获取签名
5733-
var Authorization = util.getAuth({
5734-
SecretId: params.SecretId || self.options.SecretId,
5735-
SecretKey: params.SecretKey || self.options.SecretKey,
5736-
Method: params.Method,
5737-
Key: PathName,
5738-
Query: params.Query,
5739-
Headers: params.Headers,
5740-
Expires: params.Expires
5741-
});
5742-
callback && callback({ Authorization: Authorization });
5743-
return Authorization;
5757+
return function () {
5758+
var Authorization = util.getAuth({
5759+
SecretId: params.SecretId || self.options.SecretId,
5760+
SecretKey: params.SecretKey || self.options.SecretKey,
5761+
Method: params.Method,
5762+
Key: PathName,
5763+
Query: params.Query,
5764+
Headers: params.Headers,
5765+
Expires: params.Expires
5766+
});
5767+
var AuthData = {
5768+
Authorization: Authorization,
5769+
XCosSecurityToken: self.options.XCosSecurityToken
5770+
};
5771+
callback && callback(AuthData);
5772+
return AuthData;
5773+
}();
57445774
}
57455775
return '';
57465776
}

dist/cos-js-sdk-v5.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/base.js

+61-55
Original file line numberDiff line numberDiff line change
@@ -976,39 +976,42 @@ function putObject(params, callback) {
976976
var FileSize = params.ContentLength;
977977
var onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);
978978

979-
submitRequest.call(self, {
980-
TaskId: params.TaskId,
981-
method: 'PUT',
982-
Bucket: params.Bucket,
983-
Region: params.Region,
984-
Key: params.Key,
985-
headers: params.Headers,
986-
body: params.Body,
987-
onProgress: onProgress,
988-
}, function (err, data) {
989-
if (err) {
990-
onProgress(null, true);
991-
return callback(err);
992-
}
993-
onProgress({loaded: FileSize, total: FileSize}, true);
994-
if (data && data.headers && data.headers['etag']) {
995-
var url = getUrl({
996-
ForcePathStyle: self.options.ForcePathStyle,
997-
protocol: self.options.Protocol,
998-
domain: self.options.Domain,
999-
bucket: params.Bucket,
1000-
region: params.Region,
1001-
object: params.Key,
1002-
});
1003-
url = url.substr(url.indexOf('://') + 3);
1004-
return callback(null, {
1005-
Location: url,
1006-
ETag: data.headers['etag'],
1007-
statusCode: data.statusCode,
1008-
headers: data.headers,
1009-
});
1010-
}
1011-
callback(null, data);
979+
util.getBodyMd5(self.options.UploadCheckContentMd5, params.Body, function (md5) {
980+
md5 && (params.Headers['Content-MD5'] = util.binaryBase64(md5));
981+
submitRequest.call(self, {
982+
TaskId: params.TaskId,
983+
method: 'PUT',
984+
Bucket: params.Bucket,
985+
Region: params.Region,
986+
Key: params.Key,
987+
headers: params.Headers,
988+
body: params.Body,
989+
onProgress: onProgress,
990+
}, function (err, data) {
991+
if (err) {
992+
onProgress(null, true);
993+
return callback(err);
994+
}
995+
onProgress({loaded: FileSize, total: FileSize}, true);
996+
if (data && data.headers && data.headers['etag']) {
997+
var url = getUrl({
998+
ForcePathStyle: self.options.ForcePathStyle,
999+
protocol: self.options.Protocol,
1000+
domain: self.options.Domain,
1001+
bucket: params.Bucket,
1002+
region: params.Region,
1003+
object: params.Key,
1004+
});
1005+
url = url.substr(url.indexOf('://') + 3);
1006+
return callback(null, {
1007+
Location: url,
1008+
ETag: data.headers['etag'],
1009+
statusCode: data.statusCode,
1010+
headers: data.headers,
1011+
});
1012+
}
1013+
callback(null, data);
1014+
});
10121015
});
10131016
}
10141017

@@ -1398,28 +1401,31 @@ function multipartUpload(params, callback) {
13981401

13991402
var self = this;
14001403
util.getFileSize('multipartUpload', params, function () {
1401-
submitRequest.call(self, {
1402-
TaskId: params.TaskId,
1403-
method: 'PUT',
1404-
Bucket: params.Bucket,
1405-
Region: params.Region,
1406-
Key: params.Key,
1407-
qs: {
1408-
partNumber: params['PartNumber'],
1409-
uploadId: params['UploadId'],
1410-
},
1411-
headers: params.Headers,
1412-
onProgress: params.onProgress,
1413-
body: params.Body || null
1414-
}, function (err, data) {
1415-
if (err) {
1416-
return callback(err);
1417-
}
1418-
data['headers'] = data['headers'] || {};
1419-
callback(null, {
1420-
ETag: data['headers']['etag'] || '',
1421-
statusCode: data.statusCode,
1422-
headers: data.headers,
1404+
util.getBodyMd5(self.options.UploadCheckContentMd5, params.Body, function (md5) {
1405+
md5 && (params.Headers['Content-MD5'] = util.binaryBase64(md5));
1406+
submitRequest.call(self, {
1407+
TaskId: params.TaskId,
1408+
method: 'PUT',
1409+
Bucket: params.Bucket,
1410+
Region: params.Region,
1411+
Key: params.Key,
1412+
qs: {
1413+
partNumber: params['PartNumber'],
1414+
uploadId: params['UploadId'],
1415+
},
1416+
headers: params.Headers,
1417+
onProgress: params.onProgress,
1418+
body: params.Body || null
1419+
}, function (err, data) {
1420+
if (err) {
1421+
return callback(err);
1422+
}
1423+
data['headers'] = data['headers'] || {};
1424+
callback(null, {
1425+
ETag: data['headers']['etag'] || '',
1426+
statusCode: data.statusCode,
1427+
headers: data.headers,
1428+
});
14231429
});
14241430
});
14251431
});

src/cos.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ var defaultOptions = {
2020
CopySliceSize: 1024 * 1024 * 10,
2121
ProgressInterval: 1000,
2222
UploadQueueSize: 10000,
23+
UploadIdCacheLimit: 50,
24+
UploadCheckContentMd5: false,
2325
Domain: '',
2426
ServiceDomain: '',
2527
Protocol: '',
2628
CompatibilityMode: false,
2729
ForcePathStyle: false,
2830
XCosSecurityToken: '',
29-
UploadIdCacheLimit: 50,
3031
};
3132

3233
// 对外暴露的类

0 commit comments

Comments
 (0)