Skip to content

Commit 2d2b33a

Browse files
authored
feat(node): bucket policy (#751)
1 parent a57315f commit 2d2b33a

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ All operation use es7 async/await to implement. All api is async function.
9999
- [.putBucketTags(name, tag[, options])](#putBucketTagsname-tag-options)
100100
- [.getBucketTags(name, [, options])](#getBucketTagsname-options)
101101
- [.deleteBucketTags(name, [, options])](#deleteBucketTagsname-options)
102+
- policy
103+
- [.putBucketPolicy(name, policy[, options])](#putBucketPolicyname-policy-options)
104+
- [.getBucketPolicy(name, [, options])](#getBucketPolicyname-options)
105+
- [.deleteBucketPolicy(name, [, options])](#deleteBucketPolicyname-options)
102106
- [Object Operations](#object-operations)
103107
- [.list(query[, options])](#listquery-options)
104108
- [.put(name, file[, options])](#putname-file-options)
@@ -1087,6 +1091,72 @@ Success will return:
10871091

10881092
---
10891093

1094+
### .putBucketPolicy(name, policy[, options])
1095+
1096+
Adds or modify policy for a bucket.
1097+
1098+
parameters:
1099+
1100+
- name {String} the bucket name
1101+
- policy {Object} bucket policy
1102+
- [options] {Object} optional args
1103+
1104+
Success will return:
1105+
1106+
- status {Number} response status
1107+
- res {Object} response info
1108+
1109+
example:
1110+
```js
1111+
const policy = {
1112+
Version: '1',
1113+
Statement: [
1114+
{
1115+
Action: ['oss:PutObject', 'oss:GetObject'],
1116+
Effect: 'Deny',
1117+
Principal: ['1234567890'],
1118+
Resource: ['acs:oss:*:1234567890:*/*']
1119+
}
1120+
]
1121+
};
1122+
const result = await store.putBucketPolicy(bucket, policy);
1123+
console.log(result);
1124+
```
1125+
---
1126+
1127+
### .getBucketPolicy(name[, options])
1128+
1129+
Obtains the policy for a bucket.
1130+
1131+
parameters:
1132+
1133+
- name {String} the bucket name
1134+
- [options] {Object} optional args
1135+
1136+
Success will return:
1137+
1138+
- policy {Object} the policy of bucket, if not exist, the value is null
1139+
- res {Object} response info
1140+
- status {Number} response status
1141+
1142+
---
1143+
1144+
### .deleteBucketPolicy(name[, options])
1145+
1146+
Deletes the policy added for a bucket.
1147+
1148+
parameters:
1149+
1150+
- name {String} the bucket name
1151+
- [options] {Object} optional args
1152+
1153+
Success will return:
1154+
1155+
- status {Number} response status
1156+
- res {Object} response info
1157+
1158+
---
1159+
10901160
## Object Operations
10911161

10921162
All operations function return Promise, except `signatureUrl`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const proto = exports;
2+
/**
3+
* deleteBucketPolicy
4+
* @param {String} bucketName - bucket name
5+
* @param {Object} options
6+
*/
7+
8+
proto.deleteBucketPolicy = async function deleteBucketPolicy(bucketName, options = {}) {
9+
this._checkBucketName(bucketName);
10+
11+
const params = this._bucketRequestParams('DELETE', bucketName, 'policy', options);
12+
13+
const result = await this.request(params);
14+
15+
return {
16+
status: result.status,
17+
res: result.res
18+
};
19+
};

lib/common/bucket/getBucketPolicy.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const proto = exports;
2+
/**
3+
* getBucketPolicy
4+
* @param {String} bucketName - bucket name
5+
* @param {Object} options
6+
*/
7+
8+
proto.getBucketPolicy = async function getBucketPolicy(bucketName, options = {}) {
9+
this._checkBucketName(bucketName);
10+
11+
const params = this._bucketRequestParams('GET', bucketName, 'policy', options);
12+
13+
const result = await this.request(params);
14+
15+
let policy = null;
16+
17+
if (result.res.status === 200) {
18+
policy = JSON.parse(result.res.data.toString());
19+
}
20+
21+
return {
22+
policy,
23+
status: result.status,
24+
res: result.res
25+
};
26+
};

lib/common/bucket/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ merge(proto, require('./deleteBucketWebsite'));
1818
merge(proto, require('./getBucketLifecycle'));
1919
merge(proto, require('./putBucketLifecycle'));
2020
merge(proto, require('./deleteBucketLifecycle'));
21+
merge(proto, require('./getBucketPolicy'));
22+
merge(proto, require('./putBucketPolicy'));
23+
merge(proto, require('./deleteBucketPolicy'));

lib/common/bucket/putBucketPolicy.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
const policy2Str = require('../utils/policy2Str');
3+
const isObject = require('../utils/isObject');
4+
5+
const proto = exports;
6+
/**
7+
* putBucketPolicy
8+
* @param {String} bucketName - bucket name
9+
* @param {Object} policy - bucket policy
10+
* @param {Object} options
11+
*/
12+
13+
proto.putBucketPolicy = async function putBucketPolicy(bucketName, policy, options = {}) {
14+
this._checkBucketName(bucketName);
15+
16+
if (!isObject(policy)) {
17+
throw new Error('policy is not Object');
18+
}
19+
const params = this._bucketRequestParams('PUT', bucketName, 'policy', options);
20+
params.content = policy2Str(policy);
21+
22+
const result = await this.request(params);
23+
return {
24+
status: result.status,
25+
res: result.res
26+
};
27+
};

test/node/bucket.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,4 +1129,40 @@ describe('test/bucket.test.js', () => {
11291129
assert.equal(deleteResult.res.status, 204);
11301130
});
11311131
});
1132+
1133+
describe('getBucketPolicy() putBucketPolicy() deleteBucketPolicy()', () => {
1134+
it('should put, get, delete, when policy is Object', async () => {
1135+
try {
1136+
const policy = {
1137+
Version: '1',
1138+
Statement: [
1139+
{
1140+
Action: ['oss:PutObject', 'oss:GetObject'],
1141+
Effect: 'Deny',
1142+
Principal: ['1234567890'],
1143+
Resource: ['acs:oss:*:1234567890:*/*']
1144+
}
1145+
]
1146+
};
1147+
const result = await store.putBucketPolicy(bucket, policy);
1148+
assert.strictEqual(result.status, 200);
1149+
const result1 = await store.getBucketPolicy(bucket);
1150+
assert.deepStrictEqual(policy, result1.policy);
1151+
const result2 = await store.deleteBucketPolicy(bucket);
1152+
assert.strictEqual(result2.status, 204);
1153+
const result3 = await store.getBucketPolicy(bucket);
1154+
assert.deepStrictEqual(null, result3.policy);
1155+
} catch (err) {
1156+
assert(false, err.message);
1157+
}
1158+
});
1159+
it('should throw error, when policy is not Object', async () => {
1160+
try {
1161+
await store.putBucketPolicy(bucket, 'policy');
1162+
assert(false);
1163+
} catch (err) {
1164+
assert(true);
1165+
}
1166+
});
1167+
});
11321168
});

0 commit comments

Comments
 (0)