Bucket tagging (C++ SDK)
You can use tags to mark buckets for different purposes and manage them by category.
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient instance.
-
Only the bucket owner and users who are granted the
oss:PutBucketTaggingpermission can set tags for a bucket. Otherwise, a 403 Forbidden error is returned with the error code AccessDenied. Each bucket can have up to 20 tags (key-value pairs).
Tag keys and values must be UTF-8 encoded.
A tag key can be up to 64 characters in length, is case-sensitive, and cannot be empty. The key cannot start with
http://,https://, orAliyun(case-insensitive).A tag value can be up to 128 characters in length and can be an empty string.
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 to set bucket tags, see PutBucketTags.
-
For more information about the API operation to get bucket tags, see GetBucketTags.
-
For more information about the API operation to delete bucket tags, see DeleteBucketTags.