Skip to content

Commit

Permalink
v0.5.26 add GetService/PutBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Apr 9, 2020
1 parent 8bf50c0 commit 4d78685
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 5 deletions.
23 changes: 22 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ function getAuth() {
});
}

function getService() {
cos.getService({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
}, function (err, data) {
logger.log(err || data);
});
}

function putBucket() {
cos.putBucket({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
// Prefix: 'dir/'
// Delimiter: '/'
}, function (err, data) {
logger.log(err || data);
});
}
getObject();
function getBucket() {
cos.getBucket({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Expand Down Expand Up @@ -947,7 +967,8 @@ function sliceCopyFile() {

(function () {
var list = [
// 'getService', // 不支持
'getService', // 不支持
'putBucket', // 不支持
'getObjectUrl',
'getAuth',
// 'putBucket', // 不支持
Expand Down
96 changes: 95 additions & 1 deletion dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.25';
COS.version = '0.5.26';

module.exports = COS;

Expand Down Expand Up @@ -4101,6 +4101,98 @@ var util = __webpack_require__(0);

// Bucket 相关

/**
* 获取用户的 bucket 列表
* @param {Object} params 回调函数,必须,下面为参数列表
* 无特殊参数
* @param {Function} callback 回调函数,必须
*/
function getService(params, callback) {

if (typeof params === 'function') {
callback = params;
params = {};
}
var protocol = this.options.Protocol || (util.isBrowser && location.protocol === 'http:' ? 'http:' : 'https:');
var domain = this.options.ServiceDomain;
var region = params.Region;
if (domain) {
domain = domain.replace(/\{Region\}/ig, region || '').replace(/\{.*?\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else if (region) {
domain = protocol + '//cos.' + region + '.myqcloud.com';
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
Action: 'name/cos:GetService',
url: domain,
method: 'GET',
headers: params.Headers
}, function (err, data) {
if (err) {
return callback(err);
}
var buckets = data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Buckets && data.ListAllMyBucketsResult.Buckets.Bucket || [];
buckets = util.isArray(buckets) ? buckets : [buckets];
var owner = data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Owner || {};
callback(null, {
Buckets: buckets,
Owner: owner,
statusCode: data.statusCode,
headers: data.headers
});
});
}

/**
* 创建 Bucket,并初始化访问权限
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.ACL 用户自定义文件权限,可以设置:private,public-read;默认值:private,非必须
* @param {String} params.GrantRead 赋予被授权者读的权限,格式x-cos-grant-read: uin=" ",uin=" ",非必须
* @param {String} params.GrantWrite 赋予被授权者写的权限,格式x-cos-grant-write: uin=" ",uin=" ",非必须
* @param {String} params.GrantFullControl 赋予被授权者读写权限,格式x-cos-grant-full-control: uin=" ",uin=" ",非必须
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。https://cloud.tencent.com/document/product/436/7730
* @return {Object} data 返回的数据
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {

var self = this;
submitRequest.call(this, {
Action: 'name/cos:PutBucket',
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers
}, function (err, data) {
if (err) {
return callback(err);
}
var url = getUrl({
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
isLocation: true
});
callback(null, {
Location: url,
statusCode: data.statusCode,
headers: data.headers
});
});
}

/**
* 查看是否存在该Bucket,是否有权限访问
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -6571,6 +6663,8 @@ function _submitRequest(params, callback) {

var API_MAP = {
// Bucket 相关方法
getService: getService, // Bucket
putBucket: putBucket,
headBucket: headBucket, // Bucket
getBucket: getBucket,
deleteBucket: deleteBucket,
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.5.25",
"version": "0.5.26",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
Expand Down
95 changes: 95 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,99 @@ var util = require('./util');

// Bucket 相关

/**
* 获取用户的 bucket 列表
* @param {Object} params 回调函数,必须,下面为参数列表
* 无特殊参数
* @param {Function} callback 回调函数,必须
*/
function getService(params, callback) {

if (typeof params === 'function') {
callback = params;
params = {};
}
var protocol = this.options.Protocol || (util.isBrowser && location.protocol === 'http:' ? 'http:' : 'https:');
var domain = this.options.ServiceDomain;
var region = params.Region;
if (domain) {
domain = domain.replace(/\{Region\}/ig, region || '').replace(/\{.*?\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else if(region){
domain = protocol + '//cos.'+ region + '.myqcloud.com';
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
Action: 'name/cos:GetService',
url: domain,
method: 'GET',
headers: params.Headers,
}, function (err, data) {
if (err) {
return callback(err);
}
var buckets = (data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Buckets
&& data.ListAllMyBucketsResult.Buckets.Bucket) || [];
buckets = util.isArray(buckets) ? buckets : [buckets];
var owner = (data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Owner) || {};
callback(null, {
Buckets: buckets,
Owner: owner,
statusCode: data.statusCode,
headers: data.headers,
});
});
}

/**
* 创建 Bucket,并初始化访问权限
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.ACL 用户自定义文件权限,可以设置:private,public-read;默认值:private,非必须
* @param {String} params.GrantRead 赋予被授权者读的权限,格式x-cos-grant-read: uin=" ",uin=" ",非必须
* @param {String} params.GrantWrite 赋予被授权者写的权限,格式x-cos-grant-write: uin=" ",uin=" ",非必须
* @param {String} params.GrantFullControl 赋予被授权者读写权限,格式x-cos-grant-full-control: uin=" ",uin=" ",非必须
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。https://cloud.tencent.com/document/product/436/7730
* @return {Object} data 返回的数据
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {

var self = this;
submitRequest.call(this, {
Action: 'name/cos:PutBucket',
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers,
}, function (err, data) {
if (err) {
return callback(err);
}
var url = getUrl({
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
isLocation: true,
});
callback(null, {
Location: url,
statusCode: data.statusCode,
headers: data.headers,
});
});
}

/**
* 查看是否存在该Bucket,是否有权限访问
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -2504,6 +2597,8 @@ function _submitRequest(params, callback) {

var API_MAP = {
// Bucket 相关方法
getService: getService, // Bucket
putBucket: putBucket,
headBucket: headBucket, // Bucket
getBucket: getBucket,
deleteBucket: deleteBucket,
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.25';
COS.version = '0.5.26';

module.exports = COS;

0 comments on commit 4d78685

Please sign in to comment.