Skip to content

Commit

Permalink
getObject 支持 onProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jan 12, 2021
1 parent 64d7877 commit b31690d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ function getObject() {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip',
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
}
}, function (err, data) {
logger.log(err || data);
});
Expand Down
13 changes: 10 additions & 3 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.1.5';
COS.version = '1.1.6';

module.exports = COS;

Expand Down Expand Up @@ -6528,6 +6528,7 @@ function listObjectVersions(params, callback) {
*/
function getObject(params, callback) {
var reqParams = params.Query || {};
var onProgress = util.throttleOnProgress.call(this, 0, params.onProgress);

reqParams['response-content-type'] = params['ResponseContentType'];
reqParams['response-content-language'] = params['ResponseContentLanguage'];
Expand All @@ -6547,8 +6548,10 @@ function getObject(params, callback) {
DataType: params.DataType,
headers: params.Headers,
qs: reqParams,
rawBody: true
rawBody: true,
onDownloadProgress: onProgress
}, function (err, data) {
onProgress(null, true);
if (err) {
var statusCode = err.statusCode;
if (params.Headers['If-Modified-Since'] && statusCode && statusCode === 304) {
Expand Down Expand Up @@ -7746,7 +7749,7 @@ function getAuthorizationAsync(params, callback) {
if (formatAllow) {
callback && callback(null, AuthData);
} else {
callback && callback('authorization error');
callback && callback({ error: 'authorization error' });
}
};

Expand Down Expand Up @@ -8025,6 +8028,9 @@ function _submitRequest(params, callback) {
params.onProgress({ loaded: loaded, total: contentLength });
};
}
if (params.onDownloadProgress) {
opt.onDownloadProgress = params.onDownloadProgress;
}
if (params.DataType) {
opt.dataType = params.DataType;
}
Expand Down Expand Up @@ -8308,6 +8314,7 @@ var request = function (opt, callback) {

// onprogress
if (opt.onProgress && xhr.upload) xhr.upload.onprogress = opt.onProgress;
if (opt.onDownloadProgress) xhr.onprogress = opt.onDownloadProgress;

// success 2xx/3xx/4xx
xhr.onload = function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var request = function (opt, callback) {

// onprogress
if (opt.onProgress && xhr.upload) xhr.upload.onprogress = opt.onProgress;
if (opt.onDownloadProgress) xhr.onprogress = opt.onDownloadProgress;

// success 2xx/3xx/4xx
xhr.onload = function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "1.1.5",
"version": "1.1.6",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,7 @@ function listObjectVersions(params, callback) {
*/
function getObject(params, callback) {
var reqParams = params.Query || {};
var onProgress = util.throttleOnProgress.call(this, 0, params.onProgress);

reqParams['response-content-type'] = params['ResponseContentType'];
reqParams['response-content-language'] = params['ResponseContentLanguage'];
Expand All @@ -1794,7 +1795,9 @@ function getObject(params, callback) {
headers: params.Headers,
qs: reqParams,
rawBody: true,
onDownloadProgress: onProgress,
}, function (err, data) {
onProgress(null, true);
if (err) {
var statusCode = err.statusCode;
if (params.Headers['If-Modified-Since'] && statusCode && statusCode === 304) {
Expand Down Expand Up @@ -3295,6 +3298,9 @@ function _submitRequest(params, callback) {
params.onProgress({loaded: loaded, total: contentLength});
};
}
if (params.onDownloadProgress) {
opt.onDownloadProgress = params.onDownloadProgress;
}
if (params.DataType) {
opt.dataType = params.DataType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.1.5';
COS.version = '1.1.6';

module.exports = COS;

0 comments on commit b31690d

Please sign in to comment.