Skip to content

Commit 92a9144

Browse files
committed
增加 getObjectUrl 方法
1 parent cf39be3 commit 92a9144

File tree

6 files changed

+101
-34
lines changed

6 files changed

+101
-34
lines changed

demo/demo.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ function getAuth() {
3131
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + key + '?sign=' + encodeURIComponent(auth));
3232
}
3333

34+
function getObjectUrl() {
35+
var url = cos.getObjectUrl({
36+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
37+
Region: config.Region,
38+
Key: '1mb.zip',
39+
Expires: 60,
40+
}, function (err, data) {
41+
console.log(err || data);
42+
});
43+
console.log(url);
44+
}
45+
3446
function putBucket() {
3547
cos.putBucket({
3648
Bucket: 'testnew-' + config.Bucket.substr(config.Bucket.indexOf('-') + 1),
@@ -539,6 +551,7 @@ function restartTask() {
539551

540552
getService();
541553
// getAuth();
554+
// getObjectUrl();
542555
// putBucket();
543556
// getBucket();
544557
// headBucket();

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.2.1",
3+
"version": "2.2.2",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"scripts": {

sdk/advance.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function sliceUploadFile(params, callback) {
1515
var SliceSize = params.SliceSize || this.options.ChunkSize;
1616
var AsyncLimit = params.AsyncLimit;
1717
var StorageClass = params.StorageClass || 'Standard';
18-
var SliceCount;
1918
var FileSize;
2019
var self = this;
2120

@@ -102,7 +101,6 @@ function sliceUploadFile(params, callback) {
102101

103102
// 获取上传文件大小
104103
FileSize = params.ContentLength;
105-
SliceCount = Math.ceil(FileSize / SliceSize);
106104

107105
if (FileSize === 0) {
108106
params.Body = new Buffer('');

sdk/base.js

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,6 @@ function putObject(params, callback) {
11171117
callback({error: 'lack of param ContentLength'});
11181118
return;
11191119
}
1120-
} else if (Body && typeof Body.pipe === 'string') {
1121-
headers['Content-Length'] = Body.length;
11221120
} else if (Body && typeof Body === 'string' && util.isBrowser) { // 在浏览器允许传入字符串作为内容 'hello'
11231121
headers['Content-Length'] = Body.length;
11241122
} else {
@@ -1816,17 +1814,63 @@ function multipartAbort(params, callback) {
18161814
* @param {Object} params 参数对象,必须
18171815
* @param {String} params.Method 请求方法,必须
18181816
* @param {String} params.Key object名称,必须
1817+
* @param {String} params.Expires 名超时时间,单位秒,可选
18191818
* @return {String} data 返回签名字符串
18201819
*/
18211820
function getAuth(params) {
18221821
return util.getAuth({
1823-
Method: params.Method || 'get',
1824-
Key: params.Key || '',
1822+
Method: params.Method,
1823+
Key: params.Key,
1824+
Expires: params.Expires,
18251825
SecretId: params.SecretId || this.options.SecretId || '',
18261826
SecretKey: params.SecretKey || this.options.SecretKey || ''
18271827
});
18281828
}
18291829

1830+
/**
1831+
* 获取文件下载链接
1832+
* @param {Object} params 参数对象,必须
1833+
* @param {String} params.Bucket Bucket名称,必须
1834+
* @param {String} params.Region 地域名称,必须
1835+
* @param {String} params.Key object名称,必须
1836+
* @param {String} params.Method 请求的方法,可选
1837+
* @param {String} params.Expires 签名超时时间,单位秒,可选
1838+
* @param {Function} callback 回调函数,必须
1839+
* @return {Object} err 请求失败的错误,如果请求成功,则为空。
1840+
* @return {Object} data 返回的数据
1841+
*/
1842+
function getObjectUrl(params, callback) {
1843+
var self = this;
1844+
var url = getUrl({
1845+
domain: self.options.Domain,
1846+
bucket: params.Bucket,
1847+
region: params.Region,
1848+
appId: params.AppId || self.options.AppId || '',
1849+
object: params.Key,
1850+
});
1851+
var authorization = getAuthorizationAsync.call(this, {
1852+
Method: params.Method || 'get',
1853+
Key: params.Key,
1854+
}, function (AuthData) {
1855+
if (!callback) return;
1856+
var result = {
1857+
Url: url + '?sign=' + encodeURIComponent(AuthData.Authorization),
1858+
};
1859+
AuthData.XCosSecurityToken && (result.XCosSecurityToken = AuthData.XCosSecurityToken);
1860+
AuthData.ClientIP && (result.ClientIP = AuthData.ClientIP);
1861+
AuthData.ClientUA && (result.ClientUA = AuthData.ClientUA);
1862+
AuthData.Token && (result.Token = AuthData.Token);
1863+
setTimeout(function () {
1864+
callback(null, result);
1865+
});
1866+
});
1867+
if (authorization) {
1868+
return url + '?sign=' + encodeURIComponent(authorization);
1869+
} else {
1870+
return url;
1871+
}
1872+
}
1873+
18301874

18311875
/**
18321876
* 私有方法
@@ -1910,21 +1954,18 @@ function getUrl(params) {
19101954
return url;
19111955
}
19121956

1913-
// 获取签名并发起请求
1914-
function submitRequest(params, callback) {
1957+
// 异步获取签名
1958+
function getAuthorizationAsync(params, callback) {
19151959
var self = this;
1916-
var Key = params.Key || '';
19171960
if (self.options.getAuthorization) { // 外部计算签名
19181961
self.options.getAuthorization.call(self, {
1919-
Method: params.method,
1920-
Key: Key,
1962+
Method: params.Method,
1963+
Key: params.Key,
19211964
}, function (AuthData) {
1922-
if (typeof AuthData === 'object') {
1923-
params.AuthData = AuthData;
1924-
} else {
1925-
params.AuthData = {Authorization: AuthData};
1965+
if (typeof AuthData === 'string') {
1966+
AuthData = {Authorization: AuthData};
19261967
}
1927-
_submitRequest.call(self, params, callback);
1968+
callback && callback(AuthData);
19281969
});
19291970
} else if (self.options.getSTS) { // 外部获取临时密钥
19301971
var Bucket = params.Bucket || '';
@@ -1936,17 +1977,17 @@ function submitRequest(params, callback) {
19361977
var Authorization = util.getAuth({
19371978
SecretId: StsData.SecretId,
19381979
SecretKey: StsData.SecretKey,
1939-
Method: params.method,
1940-
Key: Key,
1980+
Method: params.Method,
1981+
Key: params.Key,
19411982
});
1942-
params.AuthData = {
1983+
var AuthData = {
19431984
Authorization: Authorization,
19441985
XCosSecurityToken: StsData.XCosSecurityToken || '',
19451986
Token: StsData.Token || '',
19461987
ClientIP: StsData.ClientIP || '',
19471988
ClientUA: StsData.ClientUA || '',
19481989
};
1949-
_submitRequest.call(self, params, callback);
1990+
callback && callback(AuthData);
19501991
};
19511992
if (StsData.ExpiredTime && StsData.ExpiredTime - (Date.now() / 1000 > 60)) { // 如果缓存的临时密钥有效,并还有超过60秒有效期就直接使用
19521993
runTemp();
@@ -1962,13 +2003,26 @@ function submitRequest(params, callback) {
19622003
var Authorization = util.getAuth({
19632004
SecretId: params.SecretId || self.options.SecretId,
19642005
SecretKey: params.SecretKey || self.options.SecretKey,
1965-
Method: params.method,
1966-
Key: Key,
2006+
Method: params.Method,
2007+
Key: params.Key,
19672008
// headers: opt.headers,
19682009
});
1969-
params.AuthData = {Authorization: Authorization};
1970-
_submitRequest.call(self, params, callback);
2010+
callback && callback({Authorization: Authorization});
2011+
return Authorization;
19712012
}
2013+
return '';
2014+
}
2015+
2016+
// 获取签名并发起请求
2017+
function submitRequest(params, callback) {
2018+
var self = this;
2019+
getAuthorizationAsync.call(self, {
2020+
Method: params.method,
2021+
Key: params.Key,
2022+
}, function (AuthData) {
2023+
params.AuthData = AuthData;
2024+
_submitRequest.call(self, params, callback);
2025+
});
19722026
}
19732027

19742028
// 发起请求
@@ -2226,6 +2280,7 @@ var API_MAP = {
22262280
deleteMultipleObject: deleteMultipleObject,
22272281

22282282
// 工具方法
2283+
getObjectUrl: getObjectUrl,
22292284
getAuth: getAuth,
22302285
};
22312286

sdk/task.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ var initTask = function (cos) {
115115
var id = util.uuid();
116116
params.TaskReady && params.TaskReady(id);
117117

118-
var size = 0;
118+
var size;
119119
if (params.Body && params.Body.size !== undefined) {
120120
size = params.Body.size;
121121
} else if (params.Body && params.Body.length !== undefined) {
@@ -132,6 +132,7 @@ var initTask = function (cos) {
132132
}
133133

134134
if (params.ContentLength === undefined) params.ContentLength = size;
135+
size = size || 0;
135136
params.TaskId = id;
136137

137138
var task = {

sdk/util.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function camSafeUrlEncode(str) {
1616

1717
//测试用的key后面可以去掉
1818
var getAuth = function (opt) {
19-
2019
opt = opt || {};
2120

2221
var SecretId = opt.SecretId;
@@ -55,19 +54,20 @@ var getAuth = function (opt) {
5554

5655
// 签名有效起止时间
5756
var now = parseInt(new Date().getTime() / 1000) - 1;
58-
var expired = now;
57+
var exp = now;
5958

60-
if (opt.expires === undefined) {
61-
expired += 3600; // 签名过期时间为当前 + 3600s
59+
var Expires = opt.Expires || opt.expires;
60+
if (Expires === undefined) {
61+
exp += 900; // 签名过期时间为当前 + 900s
6262
} else {
63-
expired += (opt.expires * 1) || 0;
63+
exp += (Expires * 1) || 0;
6464
}
6565

6666
// 要用到的 Authorization 参数列表
6767
var qSignAlgorithm = 'sha1';
6868
var qAk = SecretId;
69-
var qSignTime = now + ';' + expired;
70-
var qKeyTime = now + ';' + expired;
69+
var qSignTime = now + ';' + exp;
70+
var qKeyTime = now + ';' + exp;
7171
var qHeaderList = getObjectKeys(headers).join(';').toLowerCase();
7272
var qUrlParamList = getObjectKeys(queryParams).join(';').toLowerCase();
7373

@@ -273,7 +273,7 @@ var apiWrapper = function (apiName, apiFn) {
273273
}
274274
}
275275
var res = apiFn.call(this, params, callback);
276-
if (apiName === 'getAuth') {
276+
if (apiName === 'getAuth' || apiName === 'getObjectUrl') {
277277
return res;
278278
}
279279
}

0 commit comments

Comments
 (0)