Skip to content

Commit 0daa5ae

Browse files
weiyiePeterRao
authored andcommitted
feat: support tags for bucket and object (#734)
close #663
1 parent 1580167 commit 0daa5ae

17 files changed

+682
-26
lines changed

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ All operation use es7 async/await to implement. All api is async function.
9595
- [.putBucketEncryption(name[, options])](#putbucketencryptionbucketname-options)
9696
- [.getBucketEncryption(name)](#getbucketencryptionbucketname-options)
9797
- [.deleteBucketEncryption(name)](#deletebucketencryptionbucketname-options)
98+
- tagging
99+
- [.putBucketTags(name, tag[, options])](#putBucketTagsname-tag-options)
100+
- [.getBucketTags(name, [, options])](#getBucketTagsname-options)
101+
- [.deleteBucketTags(name, [, options])](#deleteBucketTagsname-options)
98102
- [Object Operations](#object-operations)
99103
- [.list(query[, options])](#listquery-options)
100104
- [.put(name, file[, options])](#putname-file-options)
@@ -126,6 +130,9 @@ All operation use es7 async/await to implement. All api is async function.
126130
- [.listUploads(query[, options])](#listuploadsquery-options)
127131
- [.abortMultipartUpload(name, uploadId[, options])](#abortmultipartuploadname-uploadid-options)
128132
- [.calculatePostSignature(policy)](#calculatePostSignaturepolicy)
133+
- [.getObjectTagging(name, [, options])](#getObjectTaggingname-options)
134+
- [.putObjectTagging(name, tag[, options])](#putObjectTaggingname-tag-options)
135+
- [.deleteObjectTagging(name, [, options])](#deleteObjectTaggingname-options)
129136
- [RTMP Operations](#rtmp-operations)
130137
- [.putChannel(id, conf[, options])](#putchannelid-conf-options)
131138
- [.getChannel(id[, options])](#getchannelid-options)
@@ -1010,6 +1017,55 @@ Success will return:
10101017

10111018
---
10121019

1020+
### .putBucketTags(name, tag[, options])
1021+
1022+
Adds tags for a bucket or modify the tags for a bucket.
1023+
1024+
parameters:
1025+
1026+
- name {String} the object name
1027+
- tag {Object} tag, eg. `{var1: value1,var2:value2}`
1028+
- [options] {Object} optional args
1029+
1030+
Success will return:
1031+
1032+
- status {Number} response status
1033+
- res {Object} response info
1034+
1035+
---
1036+
1037+
### .getBucketTags(name[, options])
1038+
1039+
Obtains the tags for a bucket.
1040+
1041+
parameters:
1042+
1043+
- name {String} the object name
1044+
- [options] {Object} optional args
1045+
1046+
Success will return:
1047+
1048+
- tag {Object} the tag of object
1049+
- res {Object} response info
1050+
1051+
---
1052+
1053+
### .deleteBucketTags(name[, options])
1054+
1055+
Deletes the tags added for a bucket.
1056+
1057+
parameters:
1058+
1059+
- name {String} the object name
1060+
- [options] {Object} optional args
1061+
1062+
Success will return:
1063+
1064+
- status {Number} response status
1065+
- res {Object} response info
1066+
1067+
---
1068+
10131069
## Object Operations
10141070

10151071
All operations function return Promise, except `signatureUrl`.
@@ -2567,6 +2623,56 @@ Object:
25672623
- Signature {String}
25682624
- policy {Object} response info
25692625
2626+
### .getObjectTagging(name[, options])
2627+
2628+
Obtains the tags of an object.
2629+
2630+
parameters:
2631+
2632+
- name {String} the object name
2633+
- [options] {Object} optional args
2634+
2635+
Success will return the channel information.
2636+
2637+
object:
2638+
2639+
- tag {Object} the tag of object
2640+
- res {Object} response info
2641+
2642+
### .putObjectTagging(name, tag[, options])
2643+
2644+
Configures or updates the tags of an object.
2645+
2646+
parameters:
2647+
2648+
- name {String} the object name
2649+
- tag {Object} tag, eg. `{var1: value1,var2:value2}`
2650+
- [options] {Object} optional args
2651+
2652+
Success will return the channel information.
2653+
2654+
object:
2655+
2656+
- status {Number} response status
2657+
- res {Object} response info
2658+
2659+
### .deleteObjectTagging(name[, options])
2660+
2661+
Deletes the tag of a specified object.
2662+
2663+
parameters:
2664+
2665+
- name {String} the object name
2666+
- tag {Object} tag, eg. `{var1: value1,var2:value2}`
2667+
- [options] {Object} optional args
2668+
2669+
Success will return the channel information.
2670+
2671+
object:
2672+
2673+
- status {Number} response status
2674+
- res {Object} response info
2675+
25702676
## RTMP Operations
25712677
25722678
All operations function is [async], except `getRtmpUrl`.

lib/browser/object.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ proto.deleteMulti = async function deleteMulti(names, options) {
275275
};
276276

277277
merge(proto, require('../common/object/copyObject'));
278+
merge(proto, require('../common/object/getObjectTagging'));
279+
merge(proto, require('../common/object/putObjectTagging'));
280+
merge(proto, require('../common/object/deleteObjectTagging'));
278281

279282
proto.putMeta = async function putMeta(name, meta, options) {
280283
const copyResult = await this.copy(name, name, {

lib/common/bucket/deleteBucketTags.js

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

lib/common/bucket/getBucketTags.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const proto = exports;
2+
const isObject = require('../utils/isObject');
3+
/**
4+
* getBucketTags
5+
* @param {String} name - bucket name
6+
* @param {Object} options
7+
* @return {Object}
8+
*/
9+
10+
proto.getBucketTags = async function getBucketTags(name, options = {}) {
11+
this._checkBucketName(name);
12+
const params = this._bucketRequestParams('GET', name, 'tagging', options);
13+
params.successStatuses = [200];
14+
const result = await this.request(params);
15+
const Tagging = await this.parseXML(result.data);
16+
let { Tag } = Tagging.TagSet;
17+
Tag = Tag && isObject(Tag) ? [Tag] : Tag || [];
18+
19+
const tag = {};
20+
21+
Tag.forEach((item) => {
22+
tag[item.Key] = item.Value;
23+
});
24+
25+
return {
26+
status: result.status,
27+
res: result.res,
28+
tag
29+
};
30+
};

lib/common/bucket/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ const merge = require('merge-descriptors');
22

33
const proto = exports;
44

5-
merge(proto, require('./getBucketRequestPayment.js'));
6-
merge(proto, require('./putBucketRequestPayment.js'));
7-
merge(proto, require('./putBucketEncryption.js'));
8-
merge(proto, require('./getBucketEncryption.js'));
9-
merge(proto, require('./deleteBucketEncryption.js'));
5+
merge(proto, require('./getBucketRequestPayment'));
6+
merge(proto, require('./putBucketRequestPayment'));
7+
merge(proto, require('./putBucketEncryption'));
8+
merge(proto, require('./getBucketEncryption'));
9+
merge(proto, require('./deleteBucketEncryption'));
10+
merge(proto, require('./getBucketTags'));
11+
merge(proto, require('./putBucketTags'));
12+
merge(proto, require('./deleteBucketTags'));

lib/common/bucket/putBucketTags.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const obj2xml = require('../utils/obj2xml');
2+
const checkTag = require('../utils/checkBucketTag');
3+
4+
const proto = exports;
5+
/**
6+
* putBucketTags
7+
* @param {Sting} name - bucket name
8+
* @param {Object} tag - bucket tag, eg: `{a: "1", b: "2"}`
9+
* @param {Object} options
10+
*/
11+
12+
proto.putBucketTags = async function putBucketTags(name, tag, options = {}) {
13+
this._checkBucketName(name);
14+
checkTag(tag);
15+
const params = this._bucketRequestParams('PUT', name, 'tagging', options);
16+
params.successStatuses = [200];
17+
tag = Object.keys(tag).map(key => ({
18+
Key: key,
19+
Value: tag[key]
20+
}));
21+
22+
const paramXMLObj = {
23+
Tagging: {
24+
TagSet: {
25+
Tag: tag
26+
}
27+
}
28+
};
29+
30+
params.mime = 'xml';
31+
params.content = obj2xml(paramXMLObj);
32+
33+
const result = await this.request(params);
34+
return {
35+
res: result.res,
36+
status: result.status
37+
};
38+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const proto = exports;
2+
/**
3+
* deleteObjectTagging
4+
* @param {String} name - object name
5+
* @param {Object} options
6+
*/
7+
8+
proto.deleteObjectTagging = async function deleteObjectTagging(
9+
name,
10+
options = {}
11+
) {
12+
options.subres = 'tagging';
13+
name = this._objectName(name);
14+
const params = this._objectRequestParams('DELETE', name, options);
15+
params.successStatuses = [204];
16+
const result = await this.request(params);
17+
18+
return {
19+
status: result.status,
20+
res: result.res
21+
};
22+
};

lib/common/object/getObjectTagging.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const proto = exports;
2+
const isObject = require('../utils/isObject');
3+
/**
4+
* getObjectTagging
5+
* @param {String} name - object name
6+
* @param {Object} options
7+
* @return {Object}
8+
*/
9+
10+
proto.getObjectTagging = async function getObjectTagging(name, options = {}) {
11+
options.subres = 'tagging';
12+
name = this._objectName(name);
13+
const params = this._objectRequestParams('GET', name, options);
14+
params.successStatuses = [200];
15+
const result = await this.request(params);
16+
const Tagging = await this.parseXML(result.data);
17+
let { Tag } = Tagging.TagSet;
18+
Tag = Tag && isObject(Tag) ? [Tag] : Tag || [];
19+
20+
const tag = {};
21+
22+
Tag.forEach((item) => {
23+
tag[item.Key] = item.Value;
24+
});
25+
26+
return {
27+
status: result.status,
28+
res: result.res,
29+
tag
30+
};
31+
};

lib/common/object/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ merge(proto, require('./putSymlink'));
77
merge(proto, require('./getObjectMeta'));
88
merge(proto, require('./copyObject'));
99
merge(proto, require('./calculatePostSignature'));
10+
merge(proto, require('./getObjectTagging'));
11+
merge(proto, require('./putObjectTagging'));
12+
merge(proto, require('./deleteObjectTagging'));
1013

lib/common/object/putObjectTagging.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const obj2xml = require('../utils/obj2xml');
2+
const checkTag = require('../utils/checkObjectTag');
3+
4+
const proto = exports;
5+
/**
6+
* putObjectTagging
7+
* @param {Sting} name - object name
8+
* @param {Object} tag - object tag, eg: `{a: "1", b: "2"}`
9+
* @param {Object} options
10+
*/
11+
12+
proto.putObjectTagging = async function putObjectTagging(name, tag, options = {}) {
13+
checkTag(tag);
14+
15+
options.subres = 'tagging';
16+
name = this._objectName(name);
17+
const params = this._objectRequestParams('PUT', name, options);
18+
params.successStatuses = [200];
19+
tag = Object.keys(tag).map(key => ({
20+
Key: key,
21+
Value: tag[key]
22+
}));
23+
24+
const paramXMLObj = {
25+
Tagging: {
26+
TagSet: {
27+
Tag: tag
28+
}
29+
}
30+
};
31+
32+
params.mime = 'xml';
33+
params.content = obj2xml(paramXMLObj);
34+
35+
const result = await this.request(params);
36+
return {
37+
res: result.res,
38+
status: result.status
39+
};
40+
};

0 commit comments

Comments
 (0)