Skip to content

Commit 60a7f40

Browse files
committed
add object tagging interface
1 parent c8de3cc commit 60a7f40

File tree

12 files changed

+453
-0
lines changed

12 files changed

+453
-0
lines changed

demo/cos_demo.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,73 @@ void GetObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
839839
<< std::endl;
840840
}
841841

842+
// 为已存在的 Object 设置标签(Tag)
843+
void PutObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
844+
const std::string& object_name) {
845+
qcloud_cos::PutObjectTaggingReq req(bucket_name, object_name);
846+
qcloud_cos::PutObjectTaggingResp resp;
847+
std::vector<Tag> tagset;
848+
Tag tag1;
849+
tag1.SetKey("age");
850+
tag1.SetValue("19");
851+
852+
Tag tag2;
853+
tag2.SetKey("name");
854+
tag2.SetValue("xiaoming");
855+
856+
Tag tag3;
857+
tag3.SetKey("sex");
858+
tag3.SetValue("male");
859+
860+
tagset.push_back(tag1);
861+
tagset.push_back(tag2);
862+
tagset.push_back(tag3);
863+
req.SetTagSet(tagset);
864+
865+
qcloud_cos::CosResult result = cos.PutObjectTagging(req, &resp);
866+
std::cout << "===================PutBucketTagging====================="
867+
<< std::endl;
868+
PrintResult(result, resp);
869+
std::cout
870+
<< "===================================================================="
871+
<< std::endl;
872+
}
873+
874+
//查询指定Object的标签
875+
void GetObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
876+
const std::string& object_name) {
877+
qcloud_cos::GetObjectTaggingReq req(bucket_name, object_name);
878+
qcloud_cos::GetObjectTaggingResp resp;
879+
880+
qcloud_cos::CosResult result = cos.GetObjectTagging(req, &resp);
881+
std::cout << "===================GetObjectTagging====================="
882+
<< std::endl;
883+
std::vector<Tag> tagset = resp.GetTagSet();
884+
for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
885+
++it) {
886+
std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
887+
}
888+
PrintResult(result, resp);
889+
std::cout
890+
<< "===================================================================="
891+
<< std::endl;
892+
}
893+
894+
//删除指定Object的标签
895+
void DeleteObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
896+
const std::string& object_name) {
897+
qcloud_cos::DeleteObjectTaggingReq req(bucket_name, object_name);
898+
qcloud_cos::DeleteObjectTaggingResp resp;
899+
900+
qcloud_cos::CosResult result = cos.DeleteObjectTagging(req, &resp);
901+
std::cout << "===================DeleteObjectTagging====================="
902+
<< std::endl;
903+
PrintResult(result, resp);
904+
std::cout
905+
<< "===================================================================="
906+
<< std::endl;
907+
}
908+
842909
void PutObjectCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
843910
const std::string& object_name, const std::string& source) {
844911
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);

include/cos_api.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,33 @@ class CosAPI {
539539
/// \return 本次请求的调用情况(如状态码等)
540540
CosResult PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp);
541541

542+
/// \brief 已存在的Object设置标签.
543+
///
544+
/// \param req PutObjectTagging请求
545+
/// \param resp PutObjectTagging返回
546+
///
547+
/// \return 本次请求的调用情况(如状态码等)
548+
CosResult PutObjectTagging(const PutObjectTaggingReq& req,
549+
PutObjectTaggingResp* resp);
550+
551+
/// \brief 查询指定对象下已有的对象标签.
552+
///
553+
/// \param req GetObjectTagging请求
554+
/// \param resp GetObjectTagging返回
555+
///
556+
/// \return 本次请求的调用情况(如状态码等)
557+
CosResult GetObjectTagging(const GetObjectTaggingReq& req,
558+
GetObjectTaggingResp* resp);
559+
560+
/// \brief 删除指定对象下已有的对象标签.
561+
///
562+
/// \param req DeleteObjectTagging请求
563+
/// \param resp DeleteObjectTagging返回
564+
///
565+
/// \return 本次请求的调用情况(如状态码等)
566+
CosResult DeleteObjectTagging(const DeleteObjectTaggingReq& req,
567+
DeleteObjectTaggingResp* resp);
568+
542569
/// \brief 复制Object, 适用于跨园区且Object小于5G
543570
///
544571
/// \param req PutObjectCopy请求

include/cos_params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const std::string kRespHeaderXCosReqId = "x-cos-request-id";
6262
const std::string kRespHeaderXCosTraceId = "x-cos-trace-id";
6363
const std::string kRespHeaderXCosNextAppendPosition = "x-cos-next-append-position";
6464
const std::string kRespHeaderXCosContentSha1 = "x-cos-content-sha1";
65+
const std::string kRespHeaderXCosTaggingCount = "x-cos-tagging-count";
6566

6667
// doc preview response header
6768
const std::string kRespHeaderXTotalPage = "X-Total-Page";

include/op/object_op.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,33 @@ class ObjectOp : public BaseOp {
207207
/// \return 本次请求的调用情况(如状态码等)
208208
CosResult PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp);
209209

210+
/// \brief 已存在的Object设置标签.
211+
///
212+
/// \param req PutObjectTagging请求
213+
/// \param resp PutObjectTagging返回
214+
///
215+
/// \return 本次请求的调用情况(如状态码等)
216+
CosResult PutObjectTagging(const PutObjectTaggingReq& req,
217+
PutObjectTaggingResp* resp);
218+
219+
/// \brief 查询指定对象下已有的对象标签.
220+
///
221+
/// \param req GetObjectTagging请求
222+
/// \param resp GetObjectTagging返回
223+
///
224+
/// \return 本次请求的调用情况(如状态码等)
225+
CosResult GetObjectTagging(const GetObjectTaggingReq& req,
226+
GetObjectTaggingResp* resp);
227+
228+
/// \brief 删除指定对象下已有的对象标签.
229+
///
230+
/// \param req DeleteObjectTagging请求
231+
/// \param resp DeleteObjectTagging返回
232+
///
233+
/// \return 本次请求的调用情况(如状态码等)
234+
CosResult DeleteObjectTagging(const DeleteObjectTaggingReq& req,
235+
DeleteObjectTaggingResp* resp);
236+
210237
/// \brief 复制Object
211238
///
212239
/// \param req PutObjectCopy请求

include/request/object_req.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,69 @@ class PutObjectACLReq : public ObjectReq {
998998
std::vector<Grant> m_acl;
999999
};
10001000

1001+
class PutObjectTaggingReq : public ObjectReq {
1002+
public:
1003+
PutObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
1004+
: ObjectReq(bucket_name, object_name) {
1005+
SetMethod("PUT");
1006+
AddParam("tagging", "");
1007+
}
1008+
1009+
void SetTagSet(std::vector<Tag>& tagset) { m_tagset = tagset; }
1010+
1011+
std::vector<Tag> GetTagSet() { return m_tagset; }
1012+
1013+
//清除tag规则.
1014+
void ClearTagSet() {
1015+
std::vector<Tag> temp;
1016+
m_tagset.swap(temp);
1017+
}
1018+
1019+
/// 添加单个tag.
1020+
void AddTag(const Tag& tag) { m_tagset.push_back(tag); }
1021+
1022+
void SetVersionId(const std::string& str) {
1023+
AddParam("VersionId", str);
1024+
}
1025+
1026+
bool GenerateRequestBody(std::string* body) const;
1027+
1028+
virtual ~PutObjectTaggingReq() {}
1029+
1030+
private:
1031+
std::vector<Tag> m_tagset;
1032+
};
1033+
1034+
class GetObjectTaggingReq : public ObjectReq {
1035+
public:
1036+
GetObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
1037+
: ObjectReq(bucket_name, object_name) {
1038+
SetMethod("GET");
1039+
AddParam("tagging", "");
1040+
}
1041+
1042+
void SetVersionId(const std::string& str) {
1043+
AddParam("VersionId", str);
1044+
}
1045+
1046+
virtual ~GetObjectTaggingReq() {}
1047+
};
1048+
1049+
class DeleteObjectTaggingReq : public ObjectReq {
1050+
public:
1051+
DeleteObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
1052+
: ObjectReq(bucket_name, object_name) {
1053+
SetMethod("DELETE");
1054+
AddParam("tagging", "");
1055+
}
1056+
1057+
void SetVersionId(const std::string& str) {
1058+
AddParam("VersionId", str);
1059+
}
1060+
1061+
virtual ~DeleteObjectTaggingReq() {}
1062+
};
1063+
10011064
class PutObjectCopyReq : public ObjectReq {
10021065
public:
10031066
PutObjectCopyReq(const std::string& bucket_name,

include/response/base_resp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class BaseResp {
9696
std::string GetConnection() const { return GetHeader(kHttpHeaderConnection); }
9797
std::string GetDate() const { return GetHeader(kHttpHeaderDate); }
9898
std::string GetServer() const { return GetHeader(kHttpHeaderServer); }
99+
std::string GetXCosTaggingCount() const {
100+
return GetHeader(kRespHeaderXCosTaggingCount);
101+
}
99102
std::string GetContentDisposition() const {
100103
return GetHeader(kHttpHeaderContentDisposition);
101104
}

include/response/object_resp.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,42 @@ class PutObjectACLResp : public BaseResp {
350350
virtual ~PutObjectACLResp() {}
351351
};
352352

353+
class PutObjectTaggingResp : public BaseResp {
354+
public:
355+
PutObjectTaggingResp() {}
356+
virtual ~PutObjectTaggingResp() {}
357+
};
358+
359+
class GetObjectTaggingResp : public BaseResp {
360+
public:
361+
GetObjectTaggingResp() {}
362+
363+
void SetTagSet(std::vector<Tag>& tagset) { m_tagset = tagset; }
364+
365+
std::vector<Tag> GetTagSet() const { return m_tagset; }
366+
367+
//清除tag规则.
368+
void ClearTagSet() {
369+
std::vector<Tag> temp;
370+
m_tagset.swap(temp);
371+
}
372+
373+
/// 添加单个tag.
374+
void AddTag(const Tag& tag) { m_tagset.push_back(tag); }
375+
376+
virtual bool ParseFromXmlString(const std::string& body);
377+
virtual ~GetObjectTaggingResp() {}
378+
379+
private:
380+
std::vector<Tag> m_tagset;
381+
};
382+
383+
class DeleteObjectTaggingResp : public BaseResp {
384+
public:
385+
DeleteObjectTaggingResp() {}
386+
virtual ~DeleteObjectTaggingResp() {}
387+
};
388+
353389
class PutObjectCopyResp : public BaseResp {
354390
public:
355391
PutObjectCopyResp() {}

src/cos_api.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,22 @@ CosResult CosAPI::PutObjectACL(const PutObjectACLReq& req,
399399
return m_object_op.PutObjectACL(req, resp);
400400
}
401401

402+
CosResult CosAPI::PutObjectTagging(const PutObjectTaggingReq& req,
403+
PutObjectTaggingResp* resp) {
404+
return m_object_op.PutObjectTagging(req, resp);
405+
}
406+
407+
CosResult CosAPI::GetObjectTagging(const GetObjectTaggingReq& req,
408+
GetObjectTaggingResp* resp) {
409+
return m_object_op.GetObjectTagging(req, resp);
410+
}
411+
412+
413+
CosResult CosAPI::DeleteObjectTagging(const DeleteObjectTaggingReq& req,
414+
DeleteObjectTaggingResp* resp) {
415+
return m_object_op.DeleteObjectTagging(req, resp);
416+
}
417+
402418
CosResult CosAPI::PutObjectCopy(const PutObjectCopyReq& req,
403419
PutObjectCopyResp* resp) {
404420
return m_object_op.PutObjectCopy(req, resp);

src/op/object_op.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,46 @@ CosResult ObjectOp::PutObjectACL(const PutObjectACLReq& req,
868868
req_body, false, resp);
869869
}
870870

871+
CosResult ObjectOp::PutObjectTagging(const PutObjectTaggingReq& req,
872+
PutObjectTaggingResp* resp) {
873+
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
874+
req.GetBucketName());
875+
std::string path = req.GetPath();
876+
877+
std::string req_body;
878+
if (!req.GenerateRequestBody(&req_body)) {
879+
CosResult result;
880+
result.SetErrorMsg("Generate PutBucketWebsite Request Body fail.");
881+
return result;
882+
}
883+
884+
std::string raw_md5 = CodecUtil::Base64Encode(CodecUtil::RawMd5(req_body));
885+
886+
std::map<std::string, std::string> additional_headers;
887+
std::map<std::string, std::string> additional_params;
888+
additional_headers.insert(std::make_pair("Content-MD5", raw_md5));
889+
return NormalAction(host, path, req, additional_headers, additional_params,
890+
req_body, false, resp);
891+
}
892+
893+
CosResult ObjectOp::GetObjectTagging(const GetObjectTaggingReq& req,
894+
GetObjectTaggingResp* resp) {
895+
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
896+
req.GetBucketName());
897+
std::string path = req.GetPath();
898+
899+
return NormalAction(host, path, req, "", false, resp);
900+
}
901+
902+
CosResult ObjectOp::DeleteObjectTagging(const DeleteObjectTaggingReq& req,
903+
DeleteObjectTaggingResp* resp) {
904+
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
905+
req.GetBucketName());
906+
std::string path = req.GetPath();
907+
908+
return NormalAction(host, path, req, "", false, resp);
909+
}
910+
871911
CosResult ObjectOp::PutObjectCopy(const PutObjectCopyReq& req,
872912
PutObjectCopyResp* resp) {
873913
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),

src/request/object_req.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,41 @@ bool PutObjectACLReq::GenerateRequestBody(std::string* body) const {
5757
return GenerateAclRequestBody(m_owner, m_acl, body);
5858
}
5959

60+
// 设置标签集合.
61+
bool PutObjectTaggingReq::GenerateRequestBody(std::string* body) const {
62+
rapidxml::xml_document<> doc;
63+
rapidxml::xml_node<>* root_node = doc.allocate_node(
64+
rapidxml::node_element, doc.allocate_string("Tagging"), NULL);
65+
doc.append_node(root_node);
66+
rapidxml::xml_node<>* TagSet_node = doc.allocate_node(
67+
rapidxml::node_element, doc.allocate_string("TagSet"), NULL);
68+
69+
for (std::vector<Tag>::const_iterator c_itr = m_tagset.begin();
70+
c_itr != m_tagset.end(); ++c_itr) {
71+
rapidxml::xml_node<>* tag_node = doc.allocate_node(
72+
rapidxml::node_element, doc.allocate_string("Tag"), NULL);
73+
if (c_itr->HasKey()) {
74+
tag_node->append_node(
75+
doc.allocate_node(rapidxml::node_element, doc.allocate_string("Key"),
76+
doc.allocate_string(c_itr->GetKey().c_str())));
77+
} else {
78+
SDK_LOG_ERR("PutBucketTagging need to set Key.");
79+
}
80+
if (c_itr->HasValue()) {
81+
tag_node->append_node(doc.allocate_node(
82+
rapidxml::node_element, doc.allocate_string("Value"),
83+
doc.allocate_string(c_itr->GetValue().c_str())));
84+
} else {
85+
SDK_LOG_ERR("PutBucketTagging need to set Value.");
86+
}
87+
TagSet_node->append_node(tag_node);
88+
}
89+
root_node->append_node(TagSet_node);
90+
rapidxml::print(std::back_inserter(*body), doc, 0);
91+
doc.clear();
92+
return true;
93+
}
94+
6095
std::map<std::string, std::string> CopyReq::GetInitHeader() const {
6196
std::map<std::string, std::string> init_headers;
6297

0 commit comments

Comments
 (0)