本文介绍如何获取对象标签(Object Tagging)。

示例代码

以下代码用于获取对象标签信息:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息。*/
    /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* 初始化网络等资源。*/
    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);

    /* 上传文件。*/
    auto outcome = client.PutObject(request);

    /* 设置对象标签。*/
    Tagging tagging;
    tagging.addTag(Tag("key1", "value1"));
    tagging.addTag(Tag("key2", "value2"));
    auto putTaggingOutcome = client.SetObjectTagging(SetObjectTaggingRequest(BucketName, ObjectName, tagging));

    /* 获取对象标签。*/
    getTaggingOutcome = client.GetObjectTagging(GetObjectTaggingRequest(BucketName, key));

    if (!getTaggingOutcome.isSuccess()) {
        /* 异常处理。*/
        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;
        }
    }

    /* 释放网络等资源。*/
    ShutdownSdk();
    return 0;
}

相关文档

  • 关于获取对象标签的完整示例代码,请参见GitHub示例
  • 关于获取对象标签的API接口说明,请参见GetObjectTagging