Skip to content

Commit

Permalink
feat(node): bucket policy (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie authored Mar 25, 2020
1 parent a57315f commit 2d2b33a
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down
19 changes: 19 additions & 0 deletions lib/common/bucket/deleteBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -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
};
};
26 changes: 26 additions & 0 deletions lib/common/bucket/getBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -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
};
};
3 changes: 3 additions & 0 deletions lib/common/bucket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
27 changes: 27 additions & 0 deletions lib/common/bucket/putBucketPolicy.js
Original file line number Diff line number Diff line change
@@ -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
};
};
36 changes: 36 additions & 0 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
});

0 comments on commit 2d2b33a

Please sign in to comment.