Skip to content

Commit 2273a39

Browse files
luozhang002binghaiwang
authored andcommitted
Add getBucketLocation and getBucketInfo (#476)
* feat:add getBucketInfo and getBucketLoaction api * test: add getBucketLoaction and getBucketInfo test case * docs: add getBucketLocation and getBucketInfo docs
1 parent 9c5c735 commit 2273a39

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ OSS, Object Storage Service. Equal to well known Amazon [S3](http://aws.amazon.c
5454
- [.putBucket*(name, region[, options])](#putbucketname-region-options)
5555
- [.useBucket(name, region)](#usebucketname-region)
5656
- [.deleteBucket*(name, region[, options])](#deletebucketname-region-options)
57+
- [.getBucketInfo*(name)](#getbucketinfoname)
58+
- [.getBucketLocation*(name)](#getbucketlocationname)
5759
- ACL
5860
- [.putBucketACL*(name, region, acl[, options])](#putbucketaclname-region-acl-options)
5961
- [.getBucketACL*(name, region[, options])](#getbucketaclname-region-options)
@@ -323,6 +325,43 @@ example:
323325
store.useBucket('helloworld', 'oss-cn-hongkong');
324326
```
325327

328+
### .getBucketInfo(name)
329+
330+
Get bucket information,include CreationDate、ExtranetEndpoint、IntranetEndpoint、Location、Name、StorageClass、
331+
Owner、AccessControlList
332+
333+
parameters:
334+
335+
- name {String} bucket name
336+
337+
example:
338+
339+
- Use `helloworld` as the default bucket
340+
341+
```js
342+
store.getBucketInfo('helloworld').then( (res) => {
343+
console.log(res.bucket)
344+
})
345+
```
346+
347+
### .getBucketLocation(name)
348+
349+
Get bucket location
350+
351+
parameters:
352+
353+
- name {String} bucket name
354+
355+
example:
356+
357+
- Use `helloworld` as the default bucket
358+
359+
```js
360+
store.getBucketLocation('helloworld').then( (res) => {
361+
console.log(res.location)
362+
})
363+
```
364+
326365
---
327366

328367
### .putBucketACL*(name, region, acl[, options])

lib/bucket.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ proto.getBucket = function getBucket() {
7575
return this.options.bucket;
7676
};
7777

78+
proto.getBucketLocation = function* getBucketLocation(name, region, options) {
79+
name = name || this.getBucket();
80+
const params = this._bucketRequestParams('GET', name, 'location', options);
81+
params.successStatuses = [200];
82+
params.xmlResponse = true;
83+
const result = yield this.request(params);
84+
return {
85+
location:result.data,
86+
res: result.res,
87+
};
88+
}
89+
90+
proto.getBucketInfo = function* getBucketInfo(name, region, options) {
91+
name = name || this.getBucket();
92+
const params = this._bucketRequestParams('GET', name, 'bucketInfo', options);
93+
params.successStatuses = [200];
94+
params.xmlResponse = true;
95+
const result = yield this.request(params);
96+
return {
97+
bucket: result.data.Bucket,
98+
res: result.res,
99+
};
100+
}
101+
78102
proto.putBucket = function* putBucket(name, region, options) {
79103
options = options || {};
80104
const params = this._bucketRequestParams('PUT', name, '', options);

test/node/bucket.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ describe('test/bucket.test.js', () => {
8888
});
8989
});
9090

91+
describe('getBucketInfo', () => {
92+
it("it should return correct bucketInfo when bucket exist", function* () {
93+
let regionInteranl = `${this.region}-interanl`
94+
const result = yield this.store.getBucketInfo(this.bucket);
95+
assert.equal(result.res.status, 200);
96+
97+
assert.equal(result.bucket.Location, `${this.region}`);
98+
assert.equal(result.bucket.ExtranetEndpoint, `${this.region}.aliyuncs.com`);
99+
assert.equal(result.bucket.IntranetEndpoint, `${this.region}-internal.aliyuncs.com`);
100+
assert.equal(result.bucket.AccessControlList.Grant, 'private');
101+
assert.equal(result.bucket.StorageClass, 'Standard');
102+
})
103+
104+
it("it should return NoSuchBucketError when bucket not exist", function* () {
105+
yield utils.throws(function* () {
106+
yield this.store.getBucketInfo('not-exists-bucket');
107+
}.bind(this), 'NoSuchBucketError');
108+
})
109+
})
110+
111+
describe('getBucketLoaction', () => {
112+
it("it should return loaction this.region", function* () {
113+
const result = yield this.store.getBucketLocation(this.bucket);
114+
assert.equal(result.location, this.region);
115+
})
116+
117+
it("it should return NoSuchBucketError when bucket not exist", function* () {
118+
yield utils.throws(function* () {
119+
yield this.store.getBucketLocation('not-exists-bucket');
120+
}.bind(this), 'NoSuchBucketError');
121+
})
122+
})
123+
91124
describe('deleteBucket()', () => {
92125
it('should delete not exists bucket throw NoSuchBucketError', function* () {
93126
yield utils.throws(function* () {

0 commit comments

Comments
 (0)