This topic describes how to configure object tagging by using Object Storage Service (OSS) SDK for C++.
Sample code
The following code provides an example on how to query the tags of an object:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the information about the account that is used to access OSS. */
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
*content << "test cpp sdk";
PutObjectRequest request(BucketName, ObjectName, content);
/* Upload the object. */
auto outcome = client.PutObject(request);
/* Add tags to the object. */
Tagging tagging;
tagging.addTag(Tag("key1", "value1"));
tagging.addTag(Tag("key2", "value2"));
auto putTaggingOutcome = client.SetObjectTagging(SetObjectTaggingRequest(BucketName, ObjectName, tagging));
/* Query the tags of the object. */
getTaggingOutcome = client.GetObjectTagging(GetObjectTaggingRequest(BucketName, key));
if (!getTaggingOutcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "getTaggingOutcome fail" <<
",code:" << getTaggingOutcome.error().Code() <<
",message:" << getTaggingOutcome.error().Message() <<
",requestId:" << getTaggingOutcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
else {
auto taglist = getTaggingOutcome.result().Tagging();
for (const auto& tag : taglist)
{
std::cout <<"GetObjectTagging success, Key:"
<< tag.Key() << "; Value:" << tag.Value() << std::endl;
}
}
/* Release resources such as networks. */
ShutdownSdk();
return 0;
}
References
- For more information about the complete sample code that is used to query the tags of an object, visit GitHub.
- For more information about the API operation that you can call to query the tags of an object, see GetObjectTagging.