Use GetObjectTagging to retrieve the tags associated with an object.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetObjectTaggingpermission. For more information, see Attach a custom policy to a RAM user.An OSSClient instance. For setup instructions, see Create an OSSClient instance.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
Example code
The following example uploads an object, sets two tags on it, and then retrieves and prints those tags.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
#include <iostream>
#include <sstream>
int main(void)
{
/* Set yourEndpoint to the endpoint of the region where the bucket is located.
For example, for the China (Hangzhou) region: https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located.
For example, for the China (Hangzhou) region: cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object, excluding the bucket name.
For example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
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);
/* Set object tags. */
Tagging tagging;
tagging.addTag(Tag("key1", "value1"));
tagging.addTag(Tag("key2", "value2"));
auto putTaggingOutcome = client.SetObjectTagging(SetObjectTaggingRequest(BucketName, ObjectName, tagging));
/* Get object tags. */
auto getTaggingOutcome = client.GetObjectTagging(GetObjectTaggingRequest(BucketName, ObjectName));
if (!getTaggingOutcome.isSuccess()) {
/* Handle errors. */
std::cout << "getTaggingOutcome fail" <<
",code:" << getTaggingOutcome.error().Code() <<
",message:" << getTaggingOutcome.error().Message() <<
",requestId:" << getTaggingOutcome.error().RequestId() << std::endl;
return -1;
}
else {
auto taglist = getTaggingOutcome.result().Tagging();
for (const auto& tag : taglist.Tags())
{
std::cout <<"GetObjectTagging success, Key:"
<< tag.Key() << "; Value:" << tag.Value() << std::endl;
}
}
/* Release network resources. */
ShutdownSdk();
return 0;
}References
For the complete sample code, see the GitHub example.
For the API reference, see GetObjectTagging.