Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature huberyxxiao 144d3000 #151

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions demo/cos_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,73 @@ void GetObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
<< std::endl;
}

// 为已存在的 Object 设置标签(Tag)
void PutObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
qcloud_cos::PutObjectTaggingReq req(bucket_name, object_name);
qcloud_cos::PutObjectTaggingResp resp;
std::vector<Tag> tagset;
Tag tag1;
tag1.SetKey("age");
tag1.SetValue("19");

Tag tag2;
tag2.SetKey("name");
tag2.SetValue("xiaoming");

Tag tag3;
tag3.SetKey("sex");
tag3.SetValue("male");

tagset.push_back(tag1);
tagset.push_back(tag2);
tagset.push_back(tag3);
req.SetTagSet(tagset);

qcloud_cos::CosResult result = cos.PutObjectTagging(req, &resp);
std::cout << "===================PutBucketTagging====================="
<< std::endl;
PrintResult(result, resp);
std::cout
<< "===================================================================="
<< std::endl;
}

//查询指定Object的标签
void GetObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
qcloud_cos::GetObjectTaggingReq req(bucket_name, object_name);
qcloud_cos::GetObjectTaggingResp resp;

qcloud_cos::CosResult result = cos.GetObjectTagging(req, &resp);
std::cout << "===================GetObjectTagging====================="
<< std::endl;
std::vector<Tag> tagset = resp.GetTagSet();
for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
++it) {
std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
}
PrintResult(result, resp);
std::cout
<< "===================================================================="
<< std::endl;
}

//删除指定Object的标签
void DeleteObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name) {
qcloud_cos::DeleteObjectTaggingReq req(bucket_name, object_name);
qcloud_cos::DeleteObjectTaggingResp resp;

qcloud_cos::CosResult result = cos.DeleteObjectTagging(req, &resp);
std::cout << "===================DeleteObjectTagging====================="
<< std::endl;
PrintResult(result, resp);
std::cout
<< "===================================================================="
<< std::endl;
}

void PutObjectCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
const std::string& object_name, const std::string& source) {
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);
Expand Down
27 changes: 27 additions & 0 deletions include/cos_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,33 @@ class CosAPI {
/// \return 本次请求的调用情况(如状态码等)
CosResult PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp);

/// \brief 已存在的Object设置标签.
///
/// \param req PutObjectTagging请求
/// \param resp PutObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult PutObjectTagging(const PutObjectTaggingReq& req,
PutObjectTaggingResp* resp);

/// \brief 查询指定对象下已有的对象标签.
///
/// \param req GetObjectTagging请求
/// \param resp GetObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult GetObjectTagging(const GetObjectTaggingReq& req,
GetObjectTaggingResp* resp);

/// \brief 删除指定对象下已有的对象标签.
///
/// \param req DeleteObjectTagging请求
/// \param resp DeleteObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult DeleteObjectTagging(const DeleteObjectTaggingReq& req,
DeleteObjectTaggingResp* resp);

/// \brief 复制Object, 适用于跨园区且Object小于5G
///
/// \param req PutObjectCopy请求
Expand Down
1 change: 1 addition & 0 deletions include/cos_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const std::string kRespHeaderXCosReqId = "x-cos-request-id";
const std::string kRespHeaderXCosTraceId = "x-cos-trace-id";
const std::string kRespHeaderXCosNextAppendPosition = "x-cos-next-append-position";
const std::string kRespHeaderXCosContentSha1 = "x-cos-content-sha1";
const std::string kRespHeaderXCosTaggingCount = "x-cos-tagging-count";

// doc preview response header
const std::string kRespHeaderXTotalPage = "X-Total-Page";
Expand Down
27 changes: 27 additions & 0 deletions include/op/object_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,33 @@ class ObjectOp : public BaseOp {
/// \return 本次请求的调用情况(如状态码等)
CosResult PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp);

/// \brief 已存在的Object设置标签.
///
/// \param req PutObjectTagging请求
/// \param resp PutObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult PutObjectTagging(const PutObjectTaggingReq& req,
PutObjectTaggingResp* resp);

/// \brief 查询指定对象下已有的对象标签.
///
/// \param req GetObjectTagging请求
/// \param resp GetObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult GetObjectTagging(const GetObjectTaggingReq& req,
GetObjectTaggingResp* resp);

/// \brief 删除指定对象下已有的对象标签.
///
/// \param req DeleteObjectTagging请求
/// \param resp DeleteObjectTagging返回
///
/// \return 本次请求的调用情况(如状态码等)
CosResult DeleteObjectTagging(const DeleteObjectTaggingReq& req,
DeleteObjectTaggingResp* resp);

/// \brief 复制Object
///
/// \param req PutObjectCopy请求
Expand Down
63 changes: 63 additions & 0 deletions include/request/object_req.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,69 @@ class PutObjectACLReq : public ObjectReq {
std::vector<Grant> m_acl;
};

class PutObjectTaggingReq : public ObjectReq {
public:
PutObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
: ObjectReq(bucket_name, object_name) {
SetMethod("PUT");
AddParam("tagging", "");
}

void SetTagSet(std::vector<Tag>& tagset) { m_tagset = tagset; }

std::vector<Tag> GetTagSet() { return m_tagset; }

//清除tag规则.
void ClearTagSet() {
std::vector<Tag> temp;
m_tagset.swap(temp);
}

/// 添加单个tag.
void AddTag(const Tag& tag) { m_tagset.push_back(tag); }

void SetVersionId(const std::string& str) {
AddParam("VersionId", str);
}

bool GenerateRequestBody(std::string* body) const;

virtual ~PutObjectTaggingReq() {}

private:
std::vector<Tag> m_tagset;
};

class GetObjectTaggingReq : public ObjectReq {
public:
GetObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
: ObjectReq(bucket_name, object_name) {
SetMethod("GET");
AddParam("tagging", "");
}

void SetVersionId(const std::string& str) {
AddParam("VersionId", str);
}

virtual ~GetObjectTaggingReq() {}
};

class DeleteObjectTaggingReq : public ObjectReq {
public:
DeleteObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
: ObjectReq(bucket_name, object_name) {
SetMethod("DELETE");
AddParam("tagging", "");
}

void SetVersionId(const std::string& str) {
AddParam("VersionId", str);
}

virtual ~DeleteObjectTaggingReq() {}
};

class PutObjectCopyReq : public ObjectReq {
public:
PutObjectCopyReq(const std::string& bucket_name,
Expand Down
3 changes: 3 additions & 0 deletions include/response/base_resp.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class BaseResp {
std::string GetConnection() const { return GetHeader(kHttpHeaderConnection); }
std::string GetDate() const { return GetHeader(kHttpHeaderDate); }
std::string GetServer() const { return GetHeader(kHttpHeaderServer); }
std::string GetXCosTaggingCount() const {
return GetHeader(kRespHeaderXCosTaggingCount);
}
std::string GetContentDisposition() const {
return GetHeader(kHttpHeaderContentDisposition);
}
Expand Down
36 changes: 36 additions & 0 deletions include/response/object_resp.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,42 @@ class PutObjectACLResp : public BaseResp {
virtual ~PutObjectACLResp() {}
};

class PutObjectTaggingResp : public BaseResp {
public:
PutObjectTaggingResp() {}
virtual ~PutObjectTaggingResp() {}
};

class GetObjectTaggingResp : public BaseResp {
public:
GetObjectTaggingResp() {}

void SetTagSet(std::vector<Tag>& tagset) { m_tagset = tagset; }

std::vector<Tag> GetTagSet() const { return m_tagset; }

//清除tag规则.
void ClearTagSet() {
std::vector<Tag> temp;
m_tagset.swap(temp);
}

/// 添加单个tag.
void AddTag(const Tag& tag) { m_tagset.push_back(tag); }

virtual bool ParseFromXmlString(const std::string& body);
virtual ~GetObjectTaggingResp() {}

private:
std::vector<Tag> m_tagset;
};

class DeleteObjectTaggingResp : public BaseResp {
public:
DeleteObjectTaggingResp() {}
virtual ~DeleteObjectTaggingResp() {}
};

class PutObjectCopyResp : public BaseResp {
public:
PutObjectCopyResp() {}
Expand Down
16 changes: 16 additions & 0 deletions src/cos_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@ CosResult CosAPI::PutObjectACL(const PutObjectACLReq& req,
return m_object_op.PutObjectACL(req, resp);
}

CosResult CosAPI::PutObjectTagging(const PutObjectTaggingReq& req,
PutObjectTaggingResp* resp) {
return m_object_op.PutObjectTagging(req, resp);
}

CosResult CosAPI::GetObjectTagging(const GetObjectTaggingReq& req,
GetObjectTaggingResp* resp) {
return m_object_op.GetObjectTagging(req, resp);
}


CosResult CosAPI::DeleteObjectTagging(const DeleteObjectTaggingReq& req,
DeleteObjectTaggingResp* resp) {
return m_object_op.DeleteObjectTagging(req, resp);
}

CosResult CosAPI::PutObjectCopy(const PutObjectCopyReq& req,
PutObjectCopyResp* resp) {
return m_object_op.PutObjectCopy(req, resp);
Expand Down
40 changes: 40 additions & 0 deletions src/op/object_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,46 @@ CosResult ObjectOp::PutObjectACL(const PutObjectACLReq& req,
req_body, false, resp);
}

CosResult ObjectOp::PutObjectTagging(const PutObjectTaggingReq& req,
PutObjectTaggingResp* resp) {
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
req.GetBucketName());
std::string path = req.GetPath();

std::string req_body;
if (!req.GenerateRequestBody(&req_body)) {
CosResult result;
result.SetErrorMsg("Generate PutBucketWebsite Request Body fail.");
return result;
}

std::string raw_md5 = CodecUtil::Base64Encode(CodecUtil::RawMd5(req_body));

std::map<std::string, std::string> additional_headers;
std::map<std::string, std::string> additional_params;
additional_headers.insert(std::make_pair("Content-MD5", raw_md5));
return NormalAction(host, path, req, additional_headers, additional_params,
req_body, false, resp);
}

CosResult ObjectOp::GetObjectTagging(const GetObjectTaggingReq& req,
GetObjectTaggingResp* resp) {
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
req.GetBucketName());
std::string path = req.GetPath();

return NormalAction(host, path, req, "", false, resp);
}

CosResult ObjectOp::DeleteObjectTagging(const DeleteObjectTaggingReq& req,
DeleteObjectTaggingResp* resp) {
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
req.GetBucketName());
std::string path = req.GetPath();

return NormalAction(host, path, req, "", false, resp);
}

CosResult ObjectOp::PutObjectCopy(const PutObjectCopyReq& req,
PutObjectCopyResp* resp) {
std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
Expand Down
35 changes: 35 additions & 0 deletions src/request/object_req.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ bool PutObjectACLReq::GenerateRequestBody(std::string* body) const {
return GenerateAclRequestBody(m_owner, m_acl, body);
}

// 设置标签集合.
bool PutObjectTaggingReq::GenerateRequestBody(std::string* body) const {
rapidxml::xml_document<> doc;
rapidxml::xml_node<>* root_node = doc.allocate_node(
rapidxml::node_element, doc.allocate_string("Tagging"), NULL);
doc.append_node(root_node);
rapidxml::xml_node<>* TagSet_node = doc.allocate_node(
rapidxml::node_element, doc.allocate_string("TagSet"), NULL);

for (std::vector<Tag>::const_iterator c_itr = m_tagset.begin();
c_itr != m_tagset.end(); ++c_itr) {
rapidxml::xml_node<>* tag_node = doc.allocate_node(
rapidxml::node_element, doc.allocate_string("Tag"), NULL);
if (c_itr->HasKey()) {
tag_node->append_node(
doc.allocate_node(rapidxml::node_element, doc.allocate_string("Key"),
doc.allocate_string(c_itr->GetKey().c_str())));
} else {
SDK_LOG_ERR("PutBucketTagging need to set Key.");
}
if (c_itr->HasValue()) {
tag_node->append_node(doc.allocate_node(
rapidxml::node_element, doc.allocate_string("Value"),
doc.allocate_string(c_itr->GetValue().c_str())));
} else {
SDK_LOG_ERR("PutBucketTagging need to set Value.");
}
TagSet_node->append_node(tag_node);
}
root_node->append_node(TagSet_node);
rapidxml::print(std::back_inserter(*body), doc, 0);
doc.clear();
return true;
}

std::map<std::string, std::string> CopyReq::GetInitHeader() const {
std::map<std::string, std::string> init_headers;

Expand Down
Loading