All Products
Search
Document Center

Object Storage Service:Bucket tagging (C++ SDK)

Last Updated:Nov 29, 2025

You can use tags to identify Object Storage Service (OSS) buckets that are used for different purposes and manage these buckets.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • Only the bucket owner and users who are granted the oss:PutBucketTagging permission can set tags for a bucket. Otherwise, a 403 Forbidden error is returned with the error code AccessDenied.

  • You can configure up to 20 tags (key-value pairs) for each bucket.

  • The key and value of a tag must be encoded in UTF-8.

  • The key can be up to 64 characters in length. It is case-sensitive and cannot be empty. The key cannot start with http://, https://, or Aliyun. These prefixes are not case-sensitive.

  • The value of a tag can be up to 128 characters in length and can be empty.

Set bucket tags

The following code provides an example on how to configure tags for a bucket named examplebucket:

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

int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the Endpoint of the bucket's region. For example, for the China (Hangzhou) region, set the Endpoint to 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, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Set bucket tags. */
    SetBucketTaggingRequest request(BucketName);
    Tag tag1("yourTagkey1","yourTagValue1");
    Tag tag2("yourTagkey2", "yourTagValue2");
    TagSet tagset;
    tagset.push_back(tag1);
    tagset.push_back(tag2);
    Tagging taging;
    taging.setTags(tagset);
    request.setTagging(taging);
    auto outcome = client.SetBucketTagging(request);

    if (outcome.isSuccess()) {      
            std::cout << " SetBucketTagging success " << std::endl;
    }
    else {
        /* Handle exceptions. */
        std::cout << "SetBucketTagging fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Get bucket tags

The following code provides an example on how to query the tags of a bucket named examplebucket:

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

int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the Endpoint of the bucket's region. For example, for the China (Hangzhou) region, set the Endpoint to 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, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Get bucket tags. */
    GetBucketTaggingRequest request(BucketName);
    auto outcome = client.GetBucketTagging(request);

    if (outcome.isSuccess()) {      
        std::cout << " GetBucketTagging success " << std::endl;
        for (const auto& tag : outcome.result().Tagging().Tags()) {
            std::cout << "tag key:" << tag.Key() <<
            "tag value:" << tag.Value() << std::endl; 
        }
    }
    else {
        /* Handle exceptions. */
        std::cout << "GetBucketTagging fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

List buckets with a specified tag

The following code provides an example on how to list the buckets with a specific tag:

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

int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the Endpoint of the bucket's region. For example, for the China (Hangzhou) region, set the Endpoint to 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, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";  

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* List buckets that have a specified tag. */
    ListBucketsRequest request;
    Tag tag1("yourTagkey1","yourTagValue1");

    request.setTag(tag1);
    auto outcome = client.ListBuckets(request);

    if (outcome.isSuccess()) {      
        std::cout << "ListBuckets success" << std::endl;
        for (const auto& bucket : outcome.result().Buckets()) {
            std::cout << "bucket name:" << bucket.Name() << std::endl;
        }
    }
    else {
        /* Handle exceptions. */
        std::cout << "ListBuckets fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Delete all tags from a bucket

The following code shows how to delete all tags from a bucket:

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

int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the Endpoint of the bucket's region. For example, for the China (Hangzhou) region, set the Endpoint to 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, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Delete bucket tags. */
    DeleteBucketTaggingRequest request(BucketName);
    auto outcome = client.DeleteBucketTagging(request);

    if (outcome.isSuccess()) {      
            std::cout << " DeleteBucketTagging success " << std::endl;
    }
    else {
        /* Handle exceptions. */
        std::cout << "DeleteBucketTagging fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Delete specified tags from a bucket

The following code shows how to delete specified tags from a bucket:

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

int main(void)
{
      /* Initialize OSS account information. */
      
      /* Set yourEndpoint to the Endpoint of the bucket's region. For example, for the China (Hangzhou) region, set the Endpoint to 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, set the region to cn-hangzhou. */
      std::string Region = "yourRegion";
      
      /* Specify the bucket name, for example, examplebucket. */
      std::string BucketName = "examplebucket";

      /* Initialize network resources. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
      auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
      OssClient client(Endpoint, credentialsProvider, conf);
      client.SetRegion(Region);

      /* Delete bucket tags based on specified keys. */
      DeleteBucketTaggingRequest request(BucketName);
      Tagging tagging;
      TagSet tagset;
      Tag tag("project","projectone");
      tagset.push_back(tag);
      tagging.setTags(tagset);
      request.setTagging(tagging);
      auto outcome = client.DeleteBucketTagging(request);

      if (outcome.isSuccess()) {      
            std::cout << " DeleteBucketTagging success " << std::endl;
      }
      else {
        /* Handle exceptions. */
        std::cout << "DeleteBucketTagging fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
      }

      /* Release network resources. */
      ShutdownSdk();
      return 0;
}

References

  • For more information about the API operation that you can call to configure tags for a bucket, see PutBucketTags.

  • For more information about the API operation that you can call to query the tags of a bucket, see GetBucketTags.

  • For more information about the API operation that you can call to delete the tags of a bucket, see DeleteBucketTags.