Skip to content

Commit

Permalink
feat: putBucket support set acl and dataRedundancyType
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie committed Jul 24, 2020
1 parent b65857e commit 80d2253
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ parameters:
If bucket exists and not belong to current account, will throw BucketAlreadyExistsError.
If bucket not exists, will create a new bucket and set it's ACL.
- [options] {Object} optional parameters
- [acl] {String} include `private`,`public-read`,`public-read-write`
- [storageClass] {String} the storage type include (Standard,IA,Archive)
- [dataRedundancyType] {String} default `LRS`, include `LRS`,`ZRS`
- [timeout] {Number} the operation timeout
- [StorageClass] {String} the storage type include (Standard,IA,Archive)

Success will return the bucket name on `bucket` properties.

Expand Down
14 changes: 9 additions & 5 deletions lib/common/bucket/putBucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ const proto = exports;
const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
const { obj2xml } = require('../utils/obj2xml');

proto.putBucket = async function putBucket(name, options) {
proto.putBucket = async function putBucket(name, options = {}) {
_checkBucketName(name, true);
options = options || {};
const params = this._bucketRequestParams('PUT', name, '', options);

const CreateBucketConfiguration = {};
const paramlXMLObJ = {
CreateBucketConfiguration
};

if (options.StorageClass) {
CreateBucketConfiguration.StorageClass = options.StorageClass;
const storageClass = options.StorageClass || options.storageClass;
const dataRedundancyType = options.DataRedundancyType || options.dataRedundancyType;
if (storageClass || dataRedundancyType) {
storageClass && (CreateBucketConfiguration.StorageClass = storageClass);
dataRedundancyType && (CreateBucketConfiguration.DataRedundancyType = dataRedundancyType);
params.mime = 'xml';
params.content = obj2xml(paramlXMLObJ, { headers: true });
}

const { acl, headers = {} } = options;
acl && (headers['x-oss-acl'] = acl);
params.headers = headers;
params.successStatuses = [200];
const result = await this.request(params);
return {
Expand Down
23 changes: 23 additions & 0 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ describe('test/bucket.test.js', () => {
});
});

// todo resume
// it('should create an ZRS bucket', async () => {
// const ZRS_name = `ali-oss-zrs-${prefix.replace(/[/.]/g, '-').slice(0, -1)}`;
// const ZRS_put_res = await store.putBucket(ZRS_name, {
// dataRedundancyType: 'ZRS'
// });
// assert.strictEqual(ZRS_put_res.res.status, 200);
// const ZRS_get_res = await store.getBucketInfo(ZRS_name);
// assert.strictEqual(ZRS_get_res.bucket.DataRedundancyType, 'ZRS');
// await store.deleteBucket(ZRS_name);
// });

it('should create an public-read bucket', async () => {
const public_read_name = `ali-oss-zrs-${prefix.replace(/[/.]/g, '-').slice(0, -1)}`;
const public_read_name_res = await store.putBucket(public_read_name, {
acl: 'public-read'
});
assert.strictEqual(public_read_name_res.res.status, 200);
const public_read_name_get_res = await store.getBucketInfo(public_read_name);
assert.strictEqual(public_read_name_get_res.bucket.AccessControlList.Grant, 'public-read');
await store.deleteBucket(public_read_name);
});

after(async () => {
const result = await store.deleteBucket(name);
assert(result.res.status === 200 || result.res.status === 204);
Expand Down

0 comments on commit 80d2253

Please sign in to comment.