By using the bucket tagging function, you can classify your buckets to manage them. For example, you can specify that only buckets with specified tags are returned in ListBucket operations.

Usage notes

When you use bucket tagging, take note of the following items:

  • Only the bucket owner and authorized RAM users can configure tagging for the bucket. If other users attempt to configure tagging for the bucket, 403 Forbidden is returned with error code AccessDenied.
  • You can configure a maximum of 20 tags for a bucket.
    • The tag key is required. The tag key can be a maximum of 64 bytes in length and cannot start with http://, https://, or Aliyun.
    • The tag value is optional. The tag value can be a maximum of 128 bytes in length.
    • The key and value of the tag must be encoded in UTF-8.
  • PutBucketTags adopts the overwriting semantics. A newly added tag completely overwrites the existing one.

Configure bucket tagging

You can run the following code to configure tags to a bucket:

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Configure bucket tagging. */
    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;
        ShutdownSdk();
        return -1;
    }

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

Query bucket tags

You can run the following code to query the tags to a bucket:

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Query 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;
        ShutdownSdk();
        return -1;
    }

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

List buckets that have specific tags

You can run the following code to list buckets with a specified tag:

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* List buckets that have specific tags. */
    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;
        ShutdownSdk();
        return -1;
    }

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

Delete bucket tagging configurations

The following code provides an example on how to delete all tags from a bucket:

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Delete bucket tagging configurations. */
    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;
        ShutdownSdk();
        return -1;
    }

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

Delete specified tags from a bucket

The following code provides an example on how to delete specified tags from a bucket:

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

int main(void)
{
      /* Initialize the OSS account information. */
      std::string AccessKeyId = "yourAccessKeyId";
      std::string AccessKeySecret = "yourAccessKeySecret";
      std::string Endpoint = "yourEndpoint";
     std::string BucketName = "yourBucketName";

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

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

     /* Specify a key to delete specified tags from a bucket. */
      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;
        ShutdownSdk();
        return -1;
      }

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