All Products
Search
Document Center

Object Storage Service:Object tagging and lifecycle management (C++ SDK)

Last Updated:Mar 20, 2026

Use object tags as filter conditions in lifecycle rules to control which objects a rule applies to. Tags can be combined with a prefix to narrow the scope further.

Tag conditions use AND logic — a rule applies only to objects that match all specified tag key-value pairs exactly. When you combine a prefix with tags, an object must match both the prefix and all tags.

Prerequisites

Before you begin, ensure that you have:

  • An Alibaba Cloud account with an OSS bucket

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid access credentials

  • The C++ SDK installed and configured

How tag filtering works

A lifecycle rule can filter objects by:

Filter typeBehavior
Tag onlyApplies to all objects in the bucket that match the specified tag key-value pair
Tags (multiple)Applies to objects that match all specified tags (AND logic)
Prefix + tag(s)Applies to objects that match the prefix and all specified tags

Each tag match is exact — both the key and value must match.

Add a tag filter to a lifecycle rule

The following example creates a lifecycle rule with two tag conditions and an expiration of 3 days. The rule applies to objects under the test1/ prefix that have both key1=value1 and key2=value2 tags.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

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 ID. For example: cn-hangzhou */
    std::string Region = "yourRegion";
    std::string BucketName = "examplebucket";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Read access credentials from environment variables. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    SetBucketLifecycleRequest request(BucketName);

    /* Define the tag filter: both tags must match for the rule to apply. */
    Tagging tagging;
    tagging.addTag(Tag("key1", "value1"));
    tagging.addTag(Tag("key2", "value2"));

    /* Configure the lifecycle rule. */
    auto rule1 = LifecycleRule();
    rule1.setID("rule1");
    rule1.setPrefix("test1/");
    rule1.setStatus(RuleStatus::Enabled);
    rule1.setExpiration(3);
    rule1.setTags(tagging.Tags());

    LifecycleRuleList list{rule1};
    request.setLifecycleRules(list);
    auto outcome = client.SetBucketLifecycle(request);

    if (!outcome.isSuccess()) {
        std::cout << "SetBucketLifecycle fail"
                  << ", code: " << outcome.error().Code()
                  << ", message: " << outcome.error().Message()
                  << ", requestId: " << outcome.error().RequestId() << std::endl;
        return -1;
    }

    ShutdownSdk();
    return 0;
}
A tag match requires both the key and value to match exactly.

Get tag information from a lifecycle rule

The following example retrieves all lifecycle rules for a bucket and prints the tag conditions for each rule.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

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 ID. For example: cn-hangzhou */
    std::string Region = "yourRegion";
    std::string BucketName = "examplebucket";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Read access credentials from environment variables. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    auto outcome = client.GetBucketLifecycle(BucketName);

    if (outcome.isSuccess()) {
        for (auto const rule : outcome.result().LifecycleRules()) {
            std::cout << "rule: " << rule.ID()
                      << ", prefix: " << rule.Prefix()
                      << ", status: " << rule.Status()
                      << ", hasExpiration: " << rule.hasExpiration()
                      << ", hasTransitionList: " << rule.hasTransitionList() << std::endl;

            for (const auto& tag : rule.Tags()) {
                std::cout << "tag — key: " << tag.Key()
                          << ", value: " << tag.Value() << std::endl;
            }
        }
    } else {
        std::cout << "GetBucketLifecycle fail"
                  << ", code: " << outcome.error().Code()
                  << ", message: " << outcome.error().Message()
                  << ", requestId: " << outcome.error().RequestId() << std::endl;
        return -1;
    }

    ShutdownSdk();
    return 0;
}

Usage notes

  • The examples use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details, see Regions and endpoints.

  • The OSSClient instance in these examples uses an OSS endpoint. To create a client with a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

References