本文介绍如何设置对象标签(Object Tagging)。

对象标签使用一组键值对(Key-Value)来标记对象。对象标签的详情请参考开发指南的对象标签

说明
  • 您可以在上传Object时设置对象标签,也可以对已上传Object设置对象标签。设置对象标签时,若对象已有标签,则覆盖原标签。设置对象标签的详情请参考PutObjectTagging
  • 设置对象标签时要求请求者有PutObjectTagging权限。
  • 更改标签时不会更新Object的Last‑Modified时间。
  • 标签合法字符集包括大小写字母、数字、空格和以下符号:

    +‑=._:/

上传Object时添加对象标签

  • 简单上传时添加对象标签
    以下代码用于简单上传时添加对象标签:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        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);
    
        /* 设置x-oss-tagging */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        request.setTagging(tagging.toQueryParameters());
    
        /* 上传文件 */
        auto outcome = client.PutObject(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "PutObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 分片上传时添加对象标签
    以下代码用于分片上传时添加对象标签:
    #include <alibabacloud/oss/OssClient.h>
    
    int64_t getFileSize(const std::string& file)
    {
        std::fstream f(file, std::ios::in | std::ios::binary);
        f.seekg(0, f.end);
        int64_t size = f.tellg();
        f.close();
        return size;
    }
    
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        InitiateMultipartUploadRequest initUploadRequest(BucketName, ObjectName);
    
        /* 设置x-oss-tagging */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        initUploadRequest.setTagging(tagging.toQueryParameters());
    
        /* 初始化分片上传事件 */
        auto uploadIdResult = client.InitiateMultipartUpload(initUploadRequest);
        auto uploadId = uploadIdResult.result().UploadId();
        std::string fileToUpload = "yourLocalFilename";
        int64_t partSize = 100 * 1024;
        PartList partETagList;
        auto fileSize = getFileSize(fileToUpload);
        int partCount = static_cast<int>(fileSize / partSize);
        /* 计算分片个数 */
        if (fileSize % partSize != 0) {
            partCount++;
        }
    
        /* 对每一个分片进行上传 */
        for (int i = 1; i <= partCount; i++) {
            auto skipBytes = partSize * (i - 1);
            auto size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
            std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(fileToUpload, std::ios::in|std::ios::binary);
            content->seekg(skipBytes, std::ios::beg);
    
            UploadPartRequest uploadPartRequest(BucketName, ObjectName, content);
            uploadPartRequest.setContentLength(size);
            uploadPartRequest.setUploadId(uploadId);
            uploadPartRequest.setPartNumber(i);
            auto uploadPartOutcome = client.UploadPart(uploadPartRequest);
            if (uploadPartOutcome.isSuccess()) {
                Part part(i, uploadPartOutcome.result().ETag());
                partETagList.push_back(part);
            }
            else {
                std::cout << "uploadPart fail" <<
                ",code:" << uploadPartOutcome.error().Code() <<
                ",message:" << uploadPartOutcome.error().Message() <<
                ",requestId:" << uploadPartOutcome.error().RequestId() << std::endl;
            }
    
        }
    
        /* 完成分片上传 */
        CompleteMultipartUploadRequest request(BucketName, ObjectName);
        request.setUploadId(uploadId);
        request.setPartList(partETagList);
        auto outcome = client.CompleteMultipartUpload(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "CompleteMultipartUpload fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 追加上传时添加对象标签
    以下代码用于追加上传时添加对象标签:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        auto meta = ObjectMetaData();
        meta.setContentType("text/plain");
    
        /* 追加文件,默认追加position为0 */
        std::shared_ptr<std::iostream> content1 = std::make_shared<std::stringstream>();
        *content1 <<"Thank you for using Aliyun Object Storage Service!";
        AppendObjectRequest request(BucketName, ObjectName, content1, meta);
    
        /* 设置标签 */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        request.setTagging(tagging.toQueryParameters());
    
        /* 追加文件 */
        auto result = client.AppendObject(request);
    
        if (!result.isSuccess()) {
            /* 异常处理 */
            std::cout << "AppendObject fail" <<
            ",code:" << result.error().Code() <<
            ",message:" << result.error().Message() <<
            ",requestId:" << result.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 断点续传上传时添加对象标签
    以下代码用于断点续传上传时添加对象标签:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
        std::string UploadFilePath = "yourUploadfilePath";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /* 断点续传上传 */
        UploadObjectRequest request(BucketName, ObjectName, YourUploadFileName);
    
        /* 设置对象标签 */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        request.setTagging(tagging.toQueryParameters());
    
        auto outcome = client.ResumableUploadObject(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "ResumableUploadObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }

对已上传Object添加或更改对象标签

以下代码用于对已上传Object添加或更改对象标签:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

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

    if (!putTaggingOutcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "SetObjectTagging fail" <<
        ",code:" << putTaggingOutcome.error().Code() <<
        ",message:" << putTaggingOutcome.error().Message() <<
        ",requestId:" << putTaggingOutcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

拷贝Object时设置对象标签

拷贝Object时,可以指定如何设置目标Object的对象标签。取值如下:
  • Copy(默认值):复制源Object的对象标签到目标 Object。
  • Replace:忽略源Object的对象标签,直接采用请求中指定的对象标签。

以下分别提供了简单拷贝1GB以下的Object、及分片拷贝1GB以上的Object时设置对象标签的详细示例。

  • 以下代码用于简单拷贝1GB以下的Object时设置对象标签:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
        std::string CopyObjectName = "yourCopyObjectName";
    
        /* 初始化网络等资源 */
        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);
    
        /* 设置x-oss-tagging */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        request.setTagging(tagging.toQueryParameters());
    
        /* 上传文件 */
        auto outcome = client.PutObject(request);
    
        /* 拷贝文件,设置标签 */
        CopyObjectRequest cpRequest(yourBucketName, CopyObjectName);
        cpRequest.setCopySource(yourBucketName, yourObjectName);
        Tagging tagging2;
        tagging2.addTag(Tag("key1-2", "value1-2"));
        tagging2.addTag(Tag("key2-2", "value2-2"));
        tagging2.addTag(Tag("key3-2", "value3-2"));
        cpRequest.setTagging(tagging2.toQueryParameters());
    
        /* 忽略源Object的对象标签,直接替换为请求中指定的对象标签 */
        cpRequest.setTaggingDirective(CopyActionList::Replace);
    
        /* 拷贝文件 */
        auto copyoutcome = client.CopyObject(cpRequest);
        if (!copyoutcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "CopyObject fail" <<
            ",code:" << copyoutcome.error().Code() <<
            ",message:" << copyoutcome.error().Message() <<
            ",requestId:" << copyoutcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 以下代码用于分片拷贝1GB 以上的的Object时设置对象标签:
    #include <alibabacloud/oss/OssClient.h>
    #include <sstream>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string SourceBucketName = "yourSourceBucketName";
        std::string CopyBucketName = "yourCopyBucketName";
        std::string SourceObjectName = "yourSourceObjectName";
        std::string CopyObjectName = "yourCopyObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        auto getObjectMetaReq = GetObjectMetaRequest(SourceBucketName, SourceObjectName);
        auto getObjectMetaResult = GetObjectMeta(getObjectMetaReq);
        if (!getObjectMetaResult.isSuccess()) {
            std::cout << "GetObjectMeta fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
        /* 获取被拷贝文件的大小 */
        auto objectSize = getObjectMetaResult.result().ContentLength();
    
        /* 拷贝大文件 */
        ObjectMetaData metaData;
        std::stringstream ssDesc;
        ssDesc << "/" << SourceBucketName << "/" << SourceObjectName;
        std::string value = ssDesc.str();
        metaData.addHeader("x-oss-copy-source", value);
        InitiateMultipartUploadRequest initUploadRequest(BucketName, ObjectName, metaData);
    
        /* 设置x-oss-tagging */
        Tagging tagging;
        tagging.addTag(Tag("key1", "value1"));
        tagging.addTag(Tag("key2", "value2"));
        initUploadRequest.setTagging(tagging.toQueryParameters());
    
        /* 初始化分片拷贝事件 */
        auto uploadIdResult = client.InitiateMultipartUpload(initUploadRequest);
        auto uploadId = uploadIdResult.result().UploadId();
        int64_t partSize = 100 * 1024;
        PartList partETagList;
        int partCount = static_cast<int>(objectSize / partSize);
        /* 计算分片个数*/
        if (objectSize % partSize != 0) {
            partCount++;
        }
    
        /* 对每一个分片进行拷贝 */
        for (int i = 1; i <= partCount; i++) {
            auto skipBytes = partSize * (i - 1);
            auto size = (partSize < objectSize - skipBytes) ? partSize : (objectSize - skipBytes);
            auto uploadPartCopyReq = UploadPartCopyRequest(CopyBucketName, CopyObjectName, SourceBucketName, SourceObjectName,uploadId, i + 1);
            uploadPartCopyReq.setCopySourceRange(skipBytes, skipBytes + size -1);
            auto uploadPartOutcome = client.UploadPartCopy(uploadPartCopyReq);
            if (uploadPartOutcome.isSuccess()) {
                Part part(i, uploadPartOutcome.result().ETag());
                partETagList.push_back(part);
            }
            else {
                std::cout << "UploadPartCopy fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
            }
    
        }
    
        /* 完成分片拷贝 */
        CompleteMultipartUploadRequest request(CopyBucketName, CopyObjectName, partETagList, uploadId);
        auto outcome = client.CompleteMultipartUpload(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "CompleteMultipartUpload fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }

为软链接文件设置标签

以下代码用于为软链接文件设置标签:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";
    std::string LinkName = "yourLinkName";

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

    /* 创建软链接 */
    CreateSymlinkRequest csRequest(BucketName, ObjectName);
    csRequest.SetSymlinkTarget(LinkName);

    /* 设置x-oss-tagging */
    Tagging tagging;
    tagging.addTag(Tag("key1", "value1"));
    tagging.addTag(Tag("key2", "value2"));
    csRequest.setTagging(tagging.toQueryParameters());

    auto csoutcome = client.CreateSymlink(csRequest);

    if (!csoutcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "CreateSymlink fail" <<
        ",code:" << csoutcome.error().Code() <<
        ",message:" << csoutcome.error().Message() <<
        ",requestId:" << csoutcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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