Object tagging uses key-value pairs to categorize objects for lifecycle management and other purposes. You can use the OSS C++ SDK V2 to set, get, and delete object tags.
Notes
-
Before you run the sample code, replace
<region>with an actual region, such ascn-hangzhou. -
The sample code uses environment variables to obtain access credentials.
-
To set object tags, you must have the
oss:PutObjectTaggingpermission. -
To get object tags, you must have the
oss:GetObjectTaggingpermission. -
To delete object tags, you must have the
oss:DeleteObjectTaggingpermission.
Examples
Set object tags
The following code sets tags for a specified object:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
oss::models::TagSet tagSet;
tagSet.tags.push_back(oss::models::Tag{"env", "production"});
tagSet.tags.push_back(oss::models::Tag{"project", "demo"});
oss::models::Tagging tagging;
tagging.tagSet = tagSet;
auto outcome = client.putObjectTagging(
oss::models::PutObjectTaggingRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt")
.setTagging(tagging));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to set object tags"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Object tags set successfully"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId() << std::endl;
return 0;
}
Get object tags
The following code retrieves the tags of a specified object:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getObjectTagging(
oss::models::GetObjectTaggingRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get object tags"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Object tags retrieved successfully, status: " << result.getStatusCode() << std::endl;
auto& tagging = result.getTagging();
if (tagging.tagSet) {
for (const auto& tag : tagging.tagSet->tags) {
std::cout << "Tag: " << tag.key << " = " << tag.value << std::endl;
}
}
return 0;
}
Delete object tags
The following code deletes the tags of a specified object:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.deleteObjectTagging(
oss::models::DeleteObjectTaggingRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to delete object tags"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Object tags deleted successfully"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId() << std::endl;
return 0;
}
Related documentation
-
For the complete sample code for setting object tags, see PutObjectTagging.cpp.
-
For the complete sample code for getting object tags, see GetObjectTagging.cpp.
-
For the complete sample code for deleting object tags, see DeleteObjectTagging.cpp.