From c01292c239aa247f8da72552665148755eb7b82d Mon Sep 17 00:00:00 2001 From: chrisftian Date: Sat, 7 May 2022 17:59:12 +0800 Subject: [PATCH] Feat/get object support use accelerate (#145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat:getObjectUrl支持全球加速参数 --- index.d.ts | 2 ++ sdk/base.js | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index db780a6..a209576 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1884,6 +1884,8 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */ QueryString?: string, /** 签名几秒后失效,默认为900秒 */ Expires?: number, + /** 是否使用全球加速 */ + UseAccelerate?: boolean, } /** getObjectUrl 接口返回值 */ interface GetObjectUrlResult { diff --git a/sdk/base.js b/sdk/base.js index d05a9ba..b797d0e 100644 --- a/sdk/base.js +++ b/sdk/base.js @@ -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, }); @@ -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 || '', @@ -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$');