Skip to content

Commit

Permalink
support ForcePathStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Sep 11, 2018
1 parent 1adea99 commit 3d2ad98
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
12 changes: 7 additions & 5 deletions csp/csp.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ <h1>cos-js-sdk-v5</h1>

var cos = new COS({
CompatibilityMode: true,
ServiceDomain: 'http://cos.default.xxx.com',
Domain: 'http://{Bucket}.cos.{Region}.xxx.com',
getAuthorization: function () {
ServiceDomain: 'http://cos.default.example.com',
Domain: 'http://cos.{Region}.example.com', // 后缀式
ForcePathStyle: true, // 后缀式
// Domain: 'http://{Bucket}.cos.{Region}.example.com', // 前缀式
getAuthorization: function (options, callback) {
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
Expand Down Expand Up @@ -431,8 +433,8 @@ <h1>cos-js-sdk-v5</h1>

function putObject() {
// 创建测试文件
var filename = '1mb.zip';
var blob = util.createFile({size: 1024 * 1024});
var filename = '1kb.zip';
var blob = util.createFile({size: 1024});
// 调用方法
cos.putObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Expand Down
35 changes: 27 additions & 8 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,8 @@ var defaultOptions = {
ServiceDomain: '',
Protocol: '',
CompatibilityMode: false,
UploadIdCacheLimit: 50
UploadIdCacheLimit: 50,
ForcePathStyle: false
};

// 对外暴露的类
Expand Down Expand Up @@ -4796,6 +4797,7 @@ function putObject(params, callback) {
onProgress({ loaded: FileSize, total: FileSize }, true);
if (data && data.headers && data.headers['etag']) {
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -5275,6 +5277,7 @@ function multipartComplete(params, callback) {
return callback(err);
}
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -5470,6 +5473,7 @@ function getAuth(params) {
function getObjectUrl(params, callback) {
var self = this;
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: params.Protocol || self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -5577,23 +5581,32 @@ function getUrl(params) {
var protocol = params.protocol || (util.isBrowser && location.protocol === 'http:' ? 'http:' : 'https:');
if (!domain) {
if (['cn-south', 'cn-south-2', 'cn-north', 'cn-east', 'cn-southwest', 'sg'].indexOf(region) > -1) {
domain = '{Bucket}.{Region}.myqcloud.com';
domain = '{Region}.myqcloud.com';
} else {
domain = '{Bucket}.cos.{Region}.myqcloud.com';
domain = 'cos.{Region}.myqcloud.com';
}
if (!params.ForcePathStyle) {
domain = '{Bucket}.' + domain;
}
}
domain = domain.replace(/\{\{AppId\}\}/ig, appId).replace(/\{\{Bucket\}\}/ig, shortBucket).replace(/\{\{Region\}\}/ig, region).replace(/\{\{.*?\}\}/ig, '');
domain = domain.replace(/\{AppId\}/ig, appId).replace(/\{BucketName\}/ig, shortBucket).replace(/\{Bucket\}/ig, longBucket).replace(/\{Region\}/ig, region).replace(/\{.*?\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}

// 去掉域名最后的斜杆
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
var url = domain;

if (params.ForcePathStyle) {
url += '/' + longBucket;
}
url += '/';
if (object) {
url += '/' + encodeURIComponent(object).replace(/%2F/g, '/');
url += encodeURIComponent(object).replace(/%2F/g, '/');
}

if (params.isLocation) {
Expand All @@ -5610,12 +5623,17 @@ function getAuthorizationAsync(params, callback) {
self._StsMap = self._StsMap || {};
var StsData = self._StsMap[Bucket + '.' + Region] || {};

var PathName = params.Key || '';
if (self.options.ForcePathStyle && Bucket) {
PathName = Bucket + '/' + PathName;
}

var calcAuthByTmpKey = function () {
var Authorization = util.getAuth({
SecretId: StsData.TmpSecretId,
SecretKey: StsData.TmpSecretKey,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers
});
Expand All @@ -5639,7 +5657,7 @@ function getAuthorizationAsync(params, callback) {
Bucket: Bucket,
Region: Region,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers
}, function (AuthData) {
Expand Down Expand Up @@ -5670,7 +5688,7 @@ function getAuthorizationAsync(params, callback) {
SecretId: params.SecretId || self.options.SecretId,
SecretKey: params.SecretKey || self.options.SecretKey,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers,
Expires: params.Expires
Expand Down Expand Up @@ -5752,14 +5770,15 @@ function _submitRequest(params, callback) {

// url
url = url || getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: bucket,
region: region,
object: object
});
if (params.action) {
url = url + (object ? '' : '/') + '?' + params.action;
url = url + '?' + params.action;
}

var opt = {
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

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": "0.4.15",
"version": "0.4.16",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down
32 changes: 25 additions & 7 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ function putObject(params, callback) {
onProgress({loaded: FileSize, total: FileSize}, true);
if (data && data.headers && data.headers['etag']) {
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -1441,6 +1442,7 @@ function multipartComplete(params, callback) {
return callback(err);
}
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -1636,6 +1638,7 @@ function getAuth(params) {
function getObjectUrl(params, callback) {
var self = this;
var url = getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: params.Protocol || self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
Expand Down Expand Up @@ -1744,9 +1747,12 @@ function getUrl(params) {
var protocol = params.protocol || (util.isBrowser && location.protocol === 'http:' ? 'http:' : 'https:');
if (!domain) {
if (['cn-south', 'cn-south-2', 'cn-north', 'cn-east', 'cn-southwest', 'sg'].indexOf(region) > -1) {
domain = '{Bucket}.{Region}.myqcloud.com';
domain = '{Region}.myqcloud.com';
} else {
domain = '{Bucket}.cos.{Region}.myqcloud.com';
domain = 'cos.{Region}.myqcloud.com';
}
if (!params.ForcePathStyle) {
domain = '{Bucket}.' + domain;
}
}
domain = domain.replace(/\{\{AppId\}\}/ig, appId)
Expand All @@ -1761,13 +1767,19 @@ function getUrl(params) {
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}

// 去掉域名最后的斜杆
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
var url = domain;

if (params.ForcePathStyle) {
url += '/' + longBucket;
}
url += '/';
if (object) {
url += '/' + encodeURIComponent(object).replace(/%2F/g, '/');
url += encodeURIComponent(object).replace(/%2F/g, '/');
}

if (params.isLocation) {
Expand All @@ -1784,12 +1796,17 @@ function getAuthorizationAsync(params, callback) {
self._StsMap = self._StsMap || {};
var StsData = self._StsMap[Bucket + '.' + Region] || {};

var PathName = params.Key || '';
if (self.options.ForcePathStyle && Bucket) {
PathName = Bucket + '/' + PathName;
}

var calcAuthByTmpKey = function () {
var Authorization = util.getAuth({
SecretId: StsData.TmpSecretId,
SecretKey: StsData.TmpSecretKey,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers,
});
Expand All @@ -1811,7 +1828,7 @@ function getAuthorizationAsync(params, callback) {
Bucket: Bucket,
Region: Region,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers,
}, function (AuthData) {
Expand Down Expand Up @@ -1843,7 +1860,7 @@ function getAuthorizationAsync(params, callback) {
SecretId: params.SecretId || self.options.SecretId,
SecretKey: params.SecretKey || self.options.SecretKey,
Method: params.Method,
Key: params.Key || '',
Key: PathName,
Query: params.Query,
Headers: params.Headers,
Expires: params.Expires,
Expand Down Expand Up @@ -1933,14 +1950,15 @@ function _submitRequest(params, callback) {

// url
url = url || getUrl({
ForcePathStyle: self.options.ForcePathStyle,
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: bucket,
region: region,
object: object,
});
if (params.action) {
url = url + (object ? '' : '/') + '?' + params.action;
url = url + '?' + params.action;
}

var opt = {
Expand Down
1 change: 1 addition & 0 deletions src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var defaultOptions = {
Protocol: '',
CompatibilityMode: false,
UploadIdCacheLimit: 50,
ForcePathStyle: false,
};

// 对外暴露的类
Expand Down

0 comments on commit 3d2ad98

Please sign in to comment.