Skip to content

Commit

Permalink
Feat/get object support use accelerate (#145)
Browse files Browse the repository at this point in the history
* feat:getObjectUrl支持全球加速参数
  • Loading branch information
livehigh authored May 7, 2022
1 parent 213f98b commit c01292c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,8 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */
QueryString?: string,
/** 签名几秒后失效,默认为900秒 */
Expires?: number,
/** 是否使用全球加速 */
UseAccelerate?: boolean,
}
/** getObjectUrl 接口返回值 */
interface GetObjectUrlResult {
Expand Down
8 changes: 5 additions & 3 deletions sdk/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3123,12 +3123,13 @@ function getV4Auth(params) {
*/
function getObjectUrl(params, callback) {
var self = this;
var useAccelerate = params.UseAccelerate === undefined ? self.options.UseAccelerate : params.UseAccelerate;
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: params.Protocol || self.options.Protocol,
domain: params.Domain || self.options.Domain,
bucket: params.Bucket,
region: params.Region,
region: useAccelerate ? 'accelerate' : params.Region,
object: params.Key,
});

Expand All @@ -3148,7 +3149,7 @@ function getObjectUrl(params, callback) {
}

// 签名加上 Host,避免跨桶访问
var SignHost = getSignHost.call(this, {Bucket: params.Bucket, Region: params.Region, Url: url});
var SignHost = getSignHost.call(this, {Bucket: params.Bucket, Region: params.Region, UseAccelerate: params.UseAccelerate, Url: url});
var AuthData = getAuthorizationAsync.call(this, {
Action: ((params.Method || '').toUpperCase() === 'PUT' ? 'name/cos:PutObject' : 'name/cos:GetObject'),
Bucket: params.Bucket || '',
Expand Down Expand Up @@ -3317,12 +3318,13 @@ function getUrl(params) {

var getSignHost = function (opt) {
if (!opt.Bucket || !opt.Region) return '';
var useAccelerate = opt.UseAccelerate === undefined ? this.options.UseAccelerate : opt.UseAccelerate;
var url = opt.Url || getUrl({
ForcePathStyle: this.options.ForcePathStyle,
protocol: this.options.Protocol,
domain: this.options.Domain,
bucket: opt.Bucket,
region: this.options.UseAccelerate ? 'accelerate' : opt.Region,
region: useAccelerate ? 'accelerate' : opt.Region,
});
var urlHost = url.replace(/^https?:\/\/([^/]+)(\/.*)?$/, '$1');
var standardHostReg = new RegExp('^([a-z\\d-]+-\\d+\\.)?(cos|cosv6|ci|pic)\\.([a-z\\d-]+)\\.myqcloud\\.com$');
Expand Down

0 comments on commit c01292c

Please sign in to comment.