Skip to content

Commit

Permalink
Add getBucketLocation and getBucketInfo (#476)
Browse files Browse the repository at this point in the history
* feat:add getBucketInfo and getBucketLoaction api

* test: add getBucketLoaction and getBucketInfo test case

* docs: add getBucketLocation and getBucketInfo docs
  • Loading branch information
luozhang002 authored and binghaiwang committed Jun 4, 2018
1 parent 9c5c735 commit 2273a39
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ OSS, Object Storage Service. Equal to well known Amazon [S3](http://aws.amazon.c
- [.putBucket*(name, region[, options])](#putbucketname-region-options)
- [.useBucket(name, region)](#usebucketname-region)
- [.deleteBucket*(name, region[, options])](#deletebucketname-region-options)
- [.getBucketInfo*(name)](#getbucketinfoname)
- [.getBucketLocation*(name)](#getbucketlocationname)
- ACL
- [.putBucketACL*(name, region, acl[, options])](#putbucketaclname-region-acl-options)
- [.getBucketACL*(name, region[, options])](#getbucketaclname-region-options)
Expand Down Expand Up @@ -323,6 +325,43 @@ example:
store.useBucket('helloworld', 'oss-cn-hongkong');
```

### .getBucketInfo(name)

Get bucket information,include CreationDate、ExtranetEndpoint、IntranetEndpoint、Location、Name、StorageClass、
Owner、AccessControlList

parameters:

- name {String} bucket name

example:

- Use `helloworld` as the default bucket

```js
store.getBucketInfo('helloworld').then( (res) => {
console.log(res.bucket)
})
```

### .getBucketLocation(name)

Get bucket location

parameters:

- name {String} bucket name

example:

- Use `helloworld` as the default bucket

```js
store.getBucketLocation('helloworld').then( (res) => {
console.log(res.location)
})
```

---

### .putBucketACL*(name, region, acl[, options])
Expand Down
24 changes: 24 additions & 0 deletions lib/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ proto.getBucket = function getBucket() {
return this.options.bucket;
};

proto.getBucketLocation = function* getBucketLocation(name, region, options) {
name = name || this.getBucket();
const params = this._bucketRequestParams('GET', name, 'location', options);
params.successStatuses = [200];
params.xmlResponse = true;
const result = yield this.request(params);
return {
location:result.data,
res: result.res,
};
}

proto.getBucketInfo = function* getBucketInfo(name, region, options) {
name = name || this.getBucket();
const params = this._bucketRequestParams('GET', name, 'bucketInfo', options);
params.successStatuses = [200];
params.xmlResponse = true;
const result = yield this.request(params);
return {
bucket: result.data.Bucket,
res: result.res,
};
}

proto.putBucket = function* putBucket(name, region, options) {
options = options || {};
const params = this._bucketRequestParams('PUT', name, '', options);
Expand Down
33 changes: 33 additions & 0 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ describe('test/bucket.test.js', () => {
});
});

describe('getBucketInfo', () => {
it("it should return correct bucketInfo when bucket exist", function* () {
let regionInteranl = `${this.region}-interanl`
const result = yield this.store.getBucketInfo(this.bucket);
assert.equal(result.res.status, 200);

assert.equal(result.bucket.Location, `${this.region}`);
assert.equal(result.bucket.ExtranetEndpoint, `${this.region}.aliyuncs.com`);
assert.equal(result.bucket.IntranetEndpoint, `${this.region}-internal.aliyuncs.com`);
assert.equal(result.bucket.AccessControlList.Grant, 'private');
assert.equal(result.bucket.StorageClass, 'Standard');
})

it("it should return NoSuchBucketError when bucket not exist", function* () {
yield utils.throws(function* () {
yield this.store.getBucketInfo('not-exists-bucket');
}.bind(this), 'NoSuchBucketError');
})
})

describe('getBucketLoaction', () => {
it("it should return loaction this.region", function* () {
const result = yield this.store.getBucketLocation(this.bucket);
assert.equal(result.location, this.region);
})

it("it should return NoSuchBucketError when bucket not exist", function* () {
yield utils.throws(function* () {
yield this.store.getBucketLocation('not-exists-bucket');
}.bind(this), 'NoSuchBucketError');
})
})

describe('deleteBucket()', () => {
it('should delete not exists bucket throw NoSuchBucketError', function* () {
yield utils.throws(function* () {
Expand Down

0 comments on commit 2273a39

Please sign in to comment.