diff --git a/README.md b/README.md index dc60b299c..d881dc75d 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ All operation use es7 async/await to implement. All api is async function. - [.putBucketTags(name, tag[, options])](#putBucketTagsname-tag-options) - [.getBucketTags(name, [, options])](#getBucketTagsname-options) - [.deleteBucketTags(name, [, options])](#deleteBucketTagsname-options) + - policy + - [.putBucketPolicy(name, policy[, options])](#putBucketPolicyname-policy-options) + - [.getBucketPolicy(name, [, options])](#getBucketPolicyname-options) + - [.deleteBucketPolicy(name, [, options])](#deleteBucketPolicyname-options) - [Object Operations](#object-operations) - [.list(query[, options])](#listquery-options) - [.put(name, file[, options])](#putname-file-options) @@ -1087,6 +1091,72 @@ Success will return: --- +### .putBucketPolicy(name, policy[, options]) + +Adds or modify policy for a bucket. + +parameters: + +- name {String} the bucket name +- policy {Object} bucket policy +- [options] {Object} optional args + +Success will return: + +- status {Number} response status +- res {Object} response info + +example: +```js +const policy = { + Version: '1', + Statement: [ + { + Action: ['oss:PutObject', 'oss:GetObject'], + Effect: 'Deny', + Principal: ['1234567890'], + Resource: ['acs:oss:*:1234567890:*/*'] + } + ] +}; +const result = await store.putBucketPolicy(bucket, policy); +console.log(result); +``` +--- + +### .getBucketPolicy(name[, options]) + +Obtains the policy for a bucket. + +parameters: + +- name {String} the bucket name +- [options] {Object} optional args + +Success will return: + +- policy {Object} the policy of bucket, if not exist, the value is null +- res {Object} response info +- status {Number} response status + +--- + +### .deleteBucketPolicy(name[, options]) + +Deletes the policy added for a bucket. + +parameters: + +- name {String} the bucket name +- [options] {Object} optional args + +Success will return: + +- status {Number} response status +- res {Object} response info + +--- + ## Object Operations All operations function return Promise, except `signatureUrl`. diff --git a/lib/common/bucket/deleteBucketPolicy.js b/lib/common/bucket/deleteBucketPolicy.js new file mode 100644 index 000000000..052560388 --- /dev/null +++ b/lib/common/bucket/deleteBucketPolicy.js @@ -0,0 +1,19 @@ +const proto = exports; +/** + * deleteBucketPolicy + * @param {String} bucketName - bucket name + * @param {Object} options + */ + +proto.deleteBucketPolicy = async function deleteBucketPolicy(bucketName, options = {}) { + this._checkBucketName(bucketName); + + const params = this._bucketRequestParams('DELETE', bucketName, 'policy', options); + + const result = await this.request(params); + + return { + status: result.status, + res: result.res + }; +}; diff --git a/lib/common/bucket/getBucketPolicy.js b/lib/common/bucket/getBucketPolicy.js new file mode 100644 index 000000000..ccb7dbd63 --- /dev/null +++ b/lib/common/bucket/getBucketPolicy.js @@ -0,0 +1,26 @@ +const proto = exports; +/** + * getBucketPolicy + * @param {String} bucketName - bucket name + * @param {Object} options + */ + +proto.getBucketPolicy = async function getBucketPolicy(bucketName, options = {}) { + this._checkBucketName(bucketName); + + const params = this._bucketRequestParams('GET', bucketName, 'policy', options); + + const result = await this.request(params); + + let policy = null; + + if (result.res.status === 200) { + policy = JSON.parse(result.res.data.toString()); + } + + return { + policy, + status: result.status, + res: result.res + }; +}; diff --git a/lib/common/bucket/index.js b/lib/common/bucket/index.js index 557224b27..29d7017b9 100644 --- a/lib/common/bucket/index.js +++ b/lib/common/bucket/index.js @@ -18,3 +18,6 @@ merge(proto, require('./deleteBucketWebsite')); merge(proto, require('./getBucketLifecycle')); merge(proto, require('./putBucketLifecycle')); merge(proto, require('./deleteBucketLifecycle')); +merge(proto, require('./getBucketPolicy')); +merge(proto, require('./putBucketPolicy')); +merge(proto, require('./deleteBucketPolicy')); diff --git a/lib/common/bucket/putBucketPolicy.js b/lib/common/bucket/putBucketPolicy.js new file mode 100644 index 000000000..bb68c6967 --- /dev/null +++ b/lib/common/bucket/putBucketPolicy.js @@ -0,0 +1,27 @@ + +const policy2Str = require('../utils/policy2Str'); +const isObject = require('../utils/isObject'); + +const proto = exports; +/** + * putBucketPolicy + * @param {String} bucketName - bucket name + * @param {Object} policy - bucket policy + * @param {Object} options + */ + +proto.putBucketPolicy = async function putBucketPolicy(bucketName, policy, options = {}) { + this._checkBucketName(bucketName); + + if (!isObject(policy)) { + throw new Error('policy is not Object'); + } + const params = this._bucketRequestParams('PUT', bucketName, 'policy', options); + params.content = policy2Str(policy); + + const result = await this.request(params); + return { + status: result.status, + res: result.res + }; +}; diff --git a/test/node/bucket.test.js b/test/node/bucket.test.js index 27973966f..2919648a3 100644 --- a/test/node/bucket.test.js +++ b/test/node/bucket.test.js @@ -1129,4 +1129,40 @@ describe('test/bucket.test.js', () => { assert.equal(deleteResult.res.status, 204); }); }); + + describe('getBucketPolicy() putBucketPolicy() deleteBucketPolicy()', () => { + it('should put, get, delete, when policy is Object', async () => { + try { + const policy = { + Version: '1', + Statement: [ + { + Action: ['oss:PutObject', 'oss:GetObject'], + Effect: 'Deny', + Principal: ['1234567890'], + Resource: ['acs:oss:*:1234567890:*/*'] + } + ] + }; + const result = await store.putBucketPolicy(bucket, policy); + assert.strictEqual(result.status, 200); + const result1 = await store.getBucketPolicy(bucket); + assert.deepStrictEqual(policy, result1.policy); + const result2 = await store.deleteBucketPolicy(bucket); + assert.strictEqual(result2.status, 204); + const result3 = await store.getBucketPolicy(bucket); + assert.deepStrictEqual(null, result3.policy); + } catch (err) { + assert(false, err.message); + } + }); + it('should throw error, when policy is not Object', async () => { + try { + await store.putBucketPolicy(bucket, 'policy'); + assert(false); + } catch (err) { + assert(true); + } + }); + }); });