Skip to content

Commit

Permalink
add BucketEncryption APIS
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Mar 4, 2021
1 parent 801f202 commit a16493c
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 10 deletions.
37 changes: 37 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,40 @@ function listBucketInventory() {
});
}

function putBucketEncryption() {
cos.putBucketEncryption({
Bucket: config.Bucket,
Region: config.Region,
ServerSideEncryptionConfiguration: {
Rule: [{
ApplySideEncryptionConfiguration: {
SSEAlgorithm: 'AES256',
},
}],
},
}, function(err, data) {
logger.log(err || JSON.stringify(data));
});
}

function getBucketEncryption() {
cos.getBucketEncryption({
Bucket: config.Bucket,
Region: config.Region
}, function(err, data) {
logger.log(err || JSON.stringify(data));
});
}

function deleteBucketEncryption() {
cos.deleteBucketEncryption({
Bucket: config.Bucket,
Region: config.Region
}, function(err, data) {
logger.log(err || JSON.stringify(data));
});
}

function putObject() {
// 创建测试文件
var filename = '1mb.zip';
Expand Down Expand Up @@ -1392,6 +1426,9 @@ function deleteFolder() {
'getBucketInventory',
'deleteBucketInventory',
'listBucketInventory',
'putBucketEncryption',
'getBucketEncryption',
'deleteBucketEncryption',
'deleteBucket',
'putObjectCopy',
'getObject',
Expand Down
88 changes: 84 additions & 4 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.2.4';
COS.version = '1.2.5';

module.exports = COS;

Expand Down Expand Up @@ -5688,9 +5688,7 @@ function getBucketReplication(params, callback) {
}
return;
}
if (!err) {
!data.ReplicationConfiguration && (data.ReplicationConfiguration = {});
}
!data.ReplicationConfiguration && (data.ReplicationConfiguration = {});
if (data.ReplicationConfiguration.Rule) {
data.ReplicationConfiguration.Rules = util.makeArray(data.ReplicationConfiguration.Rule);
delete data.ReplicationConfiguration.Rule;
Expand Down Expand Up @@ -6478,6 +6476,85 @@ function getBucketAccelerate(params, callback) {
});
}

function putBucketEncryption(params, callback) {
var conf = params.ServerSideEncryptionConfiguration || {};
var Rules = conf.Rule || conf.Rules || [];
var xml = util.json2xml({ ServerSideEncryptionConfiguration: { Rule: Rules } });

var headers = params.Headers;
headers['Content-Type'] = 'application/xml';
headers['Content-MD5'] = util.binaryBase64(util.md5(xml));

submitRequest.call(this, {
Action: 'name/cos:PutBucketEncryption',
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
body: xml,
action: 'encryption',
headers: headers
}, function (err, data) {
if (err && err.statusCode === 204) {
return callback(null, { statusCode: err.statusCode });
} else if (err) {
return callback(err);
}
callback(null, {
statusCode: data.statusCode,
headers: data.headers
});
});
}

function getBucketEncryption(params, callback) {
submitRequest.call(this, {
Action: 'name/cos:GetBucketEncryption',
method: 'GET',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers,
action: 'encryption'
}, function (err, data) {
if (err) {
if (err.statusCode === 404 && err.code === 'NoSuchEncryptionConfiguration') {
var result = {
EncryptionConfiguration: { Rules: [] },
statusCode: err.statusCode
};
err.headers && (result.headers = err.headers);
callback(null, result);
} else {
callback(err);
}
return;
}
var Rules = util.makeArray(data.EncryptionConfiguration && data.EncryptionConfiguration.Rule || []);
data.EncryptionConfiguration = { Rules: Rules };
callback(err, data);
});
}

function deleteBucketEncryption(params, callback) {
submitRequest.call(this, {
Action: 'name/cos:DeleteBucketReplication',
method: 'DELETE',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers,
action: 'encryption'
}, function (err, data) {
if (err && err.statusCode === 204) {
return callback(null, { statusCode: err.statusCode });
} else if (err) {
return callback(err);
}
callback(null, {
statusCode: data.statusCode,
headers: data.headers
});
});
}

// Object 相关

/**
Expand Down Expand Up @@ -8219,6 +8296,9 @@ var API_MAP = {
deleteBucketInventory: deleteBucketInventory,
putBucketAccelerate: putBucketAccelerate,
getBucketAccelerate: getBucketAccelerate,
putBucketEncryption: putBucketEncryption,
getBucketEncryption: getBucketEncryption,
deleteBucketEncryption: deleteBucketEncryption,

// Object 相关方法
getObject: getObject,
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": "1.2.4",
"version": "1.2.5",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"types": "types",
Expand Down
87 changes: 84 additions & 3 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,7 @@ function getBucketReplication(params, callback) {
}
return;
}
if (!err) {
!data.ReplicationConfiguration && (data.ReplicationConfiguration = {});
}
!data.ReplicationConfiguration && (data.ReplicationConfiguration = {});
if (data.ReplicationConfiguration.Rule) {
data.ReplicationConfiguration.Rules = util.makeArray(data.ReplicationConfiguration.Rule);
delete data.ReplicationConfiguration.Rule;
Expand Down Expand Up @@ -1672,6 +1670,86 @@ function getBucketAccelerate(params, callback) {
});
}


function putBucketEncryption(params, callback) {
var conf = params.ServerSideEncryptionConfiguration || {};
var Rules = conf.Rule || conf.Rules || [];
var xml = util.json2xml({ServerSideEncryptionConfiguration: {Rule:Rules}});

var headers = params.Headers;
headers['Content-Type'] = 'application/xml';
headers['Content-MD5'] = util.binaryBase64(util.md5(xml));

submitRequest.call(this, {
Action: 'name/cos:PutBucketEncryption',
method: 'PUT',
Bucket: params.Bucket,
Region: params.Region,
body: xml,
action: 'encryption',
headers: headers,
}, function (err, data) {
if (err && err.statusCode === 204) {
return callback(null, {statusCode: err.statusCode});
} else if (err) {
return callback(err);
}
callback(null, {
statusCode: data.statusCode,
headers: data.headers,
});
});
}

function getBucketEncryption(params, callback) {
submitRequest.call(this, {
Action: 'name/cos:GetBucketEncryption',
method: 'GET',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers,
action: 'encryption',
}, function (err, data) {
if (err) {
if (err.statusCode === 404 && err.code === 'NoSuchEncryptionConfiguration') {
var result = {
EncryptionConfiguration: {Rules: []},
statusCode: err.statusCode,
};
err.headers && (result.headers = err.headers);
callback(null, result);
} else {
callback(err);
}
return;
}
var Rules = util.makeArray(data.EncryptionConfiguration && data.EncryptionConfiguration.Rule || []);
data.EncryptionConfiguration = {Rules: Rules};
callback(err, data);
});
}

function deleteBucketEncryption(params, callback) {
submitRequest.call(this, {
Action: 'name/cos:DeleteBucketReplication',
method: 'DELETE',
Bucket: params.Bucket,
Region: params.Region,
headers: params.Headers,
action: 'encryption',
}, function (err, data) {
if (err && err.statusCode === 204) {
return callback(null, {statusCode: err.statusCode});
} else if (err) {
return callback(err);
}
callback(null, {
statusCode: data.statusCode,
headers: data.headers,
});
});
}

// Object 相关

/**
Expand Down Expand Up @@ -3433,6 +3511,9 @@ var API_MAP = {
deleteBucketInventory: deleteBucketInventory,
putBucketAccelerate: putBucketAccelerate,
getBucketAccelerate: getBucketAccelerate,
putBucketEncryption: putBucketEncryption,
getBucketEncryption: getBucketEncryption,
deleteBucketEncryption: deleteBucketEncryption,

// Object 相关方法
getObject: getObject,
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 = '1.2.4';
COS.version = '1.2.5';

module.exports = COS;

0 comments on commit a16493c

Please sign in to comment.