Skip to content

Commit

Permalink
v0.3.3 新增 getObjectUrl 接口
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Dec 29, 2017
1 parent 542fc40 commit 801e677
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 81 deletions.
14 changes: 14 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ function getAuth() {
});
}

function getObjectUrl() {
var url = cos.getObjectUrl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip',
Expires: 60,
}, function (err, data) {
console.log(err || data);
});
console.log(url);
}

function getBucket() {
cos.getBucket({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Expand Down Expand Up @@ -621,6 +633,7 @@ function uploadFiles() {

// uploadFiles();
// getAuth();
// getObjectUrl();
// getBucket();
// headBucket();
// putBucketAcl();
Expand Down Expand Up @@ -662,6 +675,7 @@ function uploadFiles() {
var list = [
// 'getService', // 不支持
'getAuth',
'getObjectUrl',
// 'putBucket', // 不支持
'getBucket',
'headBucket',
Expand Down
140 changes: 101 additions & 39 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function camSafeUrlEncode(str) {

//测试用的key后面可以去掉
var getAuth = function (opt) {

opt = opt || {};

var SecretId = opt.SecretId;
Expand Down Expand Up @@ -130,19 +129,20 @@ var getAuth = function (opt) {

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

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

// 要用到的 Authorization 参数列表
var qSignAlgorithm = 'sha1';
var qAk = SecretId;
var qSignTime = now + ';' + expired;
var qKeyTime = now + ';' + expired;
var qSignTime = now + ';' + exp;
var qKeyTime = now + ';' + exp;
var qHeaderList = getObjectKeys(headers).join(';').toLowerCase();
var qUrlParamList = getObjectKeys(queryParams).join(';').toLowerCase();

Expand Down Expand Up @@ -323,23 +323,31 @@ var apiWrapper = function (apiName, apiFn) {
callback({ error: 'Region should not be start with "cos."' });
return;
}
// 兼容带有 AppId 的 Bucket
// 兼容不带 AppId 的 Bucket
var appId,
m,
bucket = params.Bucket;
if (bucket && (m = bucket.match(/^(.+)-(\d+)$/))) {
appId = m[2];
bucket = m[1];
params.AppId = appId;
params.Bucket = bucket;
if (bucket) {
if (m = bucket.match(/^(.+)-(\d+)$/)) {
appId = m[2];
bucket = m[1];
params.AppId = appId;
params.Bucket = bucket;
} else if (!params.AppId) {
if (this.options.AppId) {
params.AppId = this.options.AppId;
} else {
callback({ error: 'Bucket should format as "test-1250000000".' });
}
}
}
// 兼容带有斜杠开头的 Key
if (params.Key && params.Key.substr(0, 1) === '/') {
params.Key = params.Key.substr(1);
}
}
var res = apiFn.call(this, params, callback);
if (apiName === 'getAuth') {
if (apiName === 'getAuth' || apiName === 'getObjectUrl') {
return res;
}
};
Expand Down Expand Up @@ -2340,7 +2348,7 @@ util.extend(COS.prototype, base);
util.extend(COS.prototype, advance);

COS.getAuthorization = util.getAuth;
COS.version = '0.3.2';
COS.version = '0.3.3';

module.exports = COS;

Expand Down Expand Up @@ -3492,7 +3500,6 @@ var initTask = function (cos) {
size = params.ContentLength;
}

if (params.ContentLength === undefined) params.ContentLength = size;
params.TaskId = id;

var task = {
Expand Down Expand Up @@ -4536,7 +4543,7 @@ function putObject(params, callback) {
callback({ error: 'lack of param ContentLength' });
return;
}
} else if (Body && typeof Body.pipe === 'string' && util.isBrowser) {
} else if (Body && typeof Body === 'string' && util.isBrowser) {
// 在浏览器允许传入字符串作为内容 'hello'
headers['Content-Length'] = Body.length;
} else {
Expand Down Expand Up @@ -5230,17 +5237,63 @@ function multipartAbort(params, callback) {
* @param {Object} params 参数对象,必须
* @param {String} params.Method 请求方法,必须
* @param {String} params.Key object名称,必须
* @param {String} params.Expires 名超时时间,单位秒,可选
* @return {String} data 返回签名字符串
*/
function getAuth(params) {
return util.getAuth({
Method: params.Method || 'get',
Key: params.Key || '',
Method: params.Method,
Key: params.Key,
Expires: params.Expires,
SecretId: params.SecretId || this.options.SecretId || '',
SecretKey: params.SecretKey || this.options.SecretKey || ''
});
}

/**
* 获取文件下载链接
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.Key object名称,必须
* @param {String} params.Method 请求的方法,可选
* @param {String} params.Expires 签名超时时间,单位秒,可选
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。
* @return {Object} data 返回的数据
*/
function getObjectUrl(params, callback) {
var self = this;
var url = getUrl({
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
appId: params.AppId || self.options.AppId || '',
object: params.Key
});
var authorization = getAuthorizationAsync.call(this, {
Method: params.Method || 'get',
Key: params.Key
}, function (AuthData) {
if (!callback) return;
var result = {
Url: url + '?sign=' + encodeURIComponent(AuthData.Authorization)
};
AuthData.XCosSecurityToken && (result.XCosSecurityToken = AuthData.XCosSecurityToken);
AuthData.ClientIP && (result.ClientIP = AuthData.ClientIP);
AuthData.ClientUA && (result.ClientUA = AuthData.ClientUA);
AuthData.Token && (result.Token = AuthData.Token);
setTimeout(function () {
callback(null, result);
});
});
if (authorization) {
return url + '?sign=' + encodeURIComponent(authorization);
} else {
return url;
}
}

/**
* 私有方法
*/
Expand Down Expand Up @@ -5320,24 +5373,19 @@ function getUrl(params) {
return url;
}

// 获取签名并发起请求
function submitRequest(params, callback) {
// 异步获取签名
function getAuthorizationAsync(params, callback) {
var self = this;
var Key = params.Key || '';
if (self.options.getAuthorization) {
// 外部计算签名
self.options.getAuthorization.call(self, {
Method: params.method,
Key: Key,
method: params.method,
pathname: '/' + Key
Method: params.Method,
Key: params.Key
}, function (AuthData) {
if (typeof AuthData === 'object') {
params.AuthData = AuthData;
} else {
params.AuthData = { Authorization: AuthData };
if (typeof AuthData === 'string') {
AuthData = { Authorization: AuthData };
}
_submitRequest.call(self, params, callback);
callback && callback(AuthData);
});
} else if (self.options.getSTS) {
// 外部获取临时密钥
Expand All @@ -5350,17 +5398,17 @@ function submitRequest(params, callback) {
var Authorization = util.getAuth({
SecretId: StsData.SecretId,
SecretKey: StsData.SecretKey,
Method: params.method,
Key: Key
Method: params.Method,
Key: params.Key
});
params.AuthData = {
var AuthData = {
Authorization: Authorization,
XCosSecurityToken: StsData.XCosSecurityToken || '',
Token: StsData.Token || '',
ClientIP: StsData.ClientIP || '',
ClientUA: StsData.ClientUA || ''
};
_submitRequest.call(self, params, callback);
callback && callback(AuthData);
};
if (StsData.ExpiredTime && StsData.ExpiredTime - (Date.now() / 1000 > 60)) {
// 如果缓存的临时密钥有效,并还有超过60秒有效期就直接使用
Expand All @@ -5379,13 +5427,26 @@ function submitRequest(params, callback) {
var Authorization = util.getAuth({
SecretId: params.SecretId || self.options.SecretId,
SecretKey: params.SecretKey || self.options.SecretKey,
Method: params.method,
Key: Key
Method: params.Method,
Key: params.Key
// headers: opt.headers,
});
params.AuthData = { Authorization: Authorization };
_submitRequest.call(self, params, callback);
callback && callback({ Authorization: Authorization });
return Authorization;
}
return '';
}

// 获取签名并发起请求
function submitRequest(params, callback) {
var self = this;
getAuthorizationAsync.call(self, {
Method: params.method,
Key: params.Key
}, function (AuthData) {
params.AuthData = AuthData;
_submitRequest.call(self, params, callback);
});
}

// 发起请求
Expand Down Expand Up @@ -5552,6 +5613,7 @@ var API_MAP = {
deleteMultipleObject: deleteMultipleObject,

// 工具方法
getObjectUrl: getObjectUrl,
getAuth: getAuth
};

Expand Down
Loading

0 comments on commit 801e677

Please sign in to comment.