OSS支持使用对象标签(Object Tagging)对存储空间(Bucket)中的文件(Object)进行分类,您可以针对相同标签的Object设置生命周期规则、访问权限等。

说明 关于对象标签的更多信息,请参见对象标签

上传Object时添加对象标签

  • 简单上传时添加对象标签
    以下代码用于简单上传时添加对象标签:
    #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);
    
        /* 设置标签。*/
        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账号信息。*/
        /* 阿里云账号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);
        InitiateMultipartUploadRequest initUploadRequest(BucketName, ObjectName);
    
        /* 设置标签。*/
        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账号信息。*/
        /* 阿里云账号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);
    
        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账号信息。*/
        /* 阿里云账号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";
        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账号信息。*/
    /* 阿里云账号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));

    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的对象标签,直接采用请求中指定的对象标签。

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

  • 以下代码用于简单拷贝1 GB以下的Object时设置对象标签:
    #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";
        /* 填写源Object的完整路径。
        std::string CopyObjectName = "scrdir/scrobject.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);
    
        /* 设置标签。*/
        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;
    }
  • 以下代码用于分片拷贝1 GB 以上的Object时设置对象标签:
    #include <alibabacloud/oss/OssClient.h>
    #include <sstream>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS账号信息 */    
        /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* 填写源Bucket名称,例如srcexamplebucket。*/
        std::string SourceBucketName = "srcexamplebucket";
        /* 填写与源Bucket处于同一地域的目标Bucket名称,例如destbucket。*/
        std::string CopyBucketName = "destbucket";
        /* 填写源Object的完整路径,完整路径中不能包含Bucket名称,例如srcdir/scrobject.txt。*/
        std::string SourceObjectName = "srcdir/scrobject.txt";
        /* 填写目标Object的完整路径,完整路径中不能包含Bucket名称,例如destdir/destobject.txt。*/
        std::string CopyObjectName = "destdir/destobject.txt";
    
        /* 初始化网络等资源。*/
        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);
    
        /* 设置标签。*/
        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账号信息。*/
    /* 阿里云账号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";
    /* 填写软链接名称,例如symlink.txt。
    std::string LinkName = "symlink.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);

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

    /* 设置标签。*/
    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;
}

相关文档

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