存储空间标签
您可以通过存储空间(Bucket)的标签功能, 对Bucket进行分类管理,如ListBucket时只显示带有指定标签的Bucket。
说明
- 只有Bucket的拥有者及授权子账户才能为Bucket设置用户标签,否则返回403 Forbidden错误,错误码:AccessDenied。
- 最多可设置20对Bucket用户标签(Key-Value对)。
- Key最大长度为64字节,不能以
http ://
、https://
、Aliyun
为前缀,且不能为空。 - Value最大长度为128字节,可以为空。
- Key和Value必须为UTF-8编码。
- PutBucketTagging是覆盖语义,即新设置的标签会完全覆盖已有的标签。
设置Bucket标签
以下代码用于设置Bucket标签:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*初始化OSS账号信息*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*初始化网络等资源*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*设置Bucket标签*/
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 {
/*异常处理*/
std::cout << "SetBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*释放网络等资源*/
ShutdownSdk();
return 0;
}
获取Bucket标签
以下代码用于获取Bucket标签:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*初始化OSS账号信息*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*初始化网络等资源*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*获取Bucket标签*/
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 {
/*异常处理*/
std::cout << "GetBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*释放网络等资源*/
ShutdownSdk();
return 0;
}
列举带指定标签的Bucket
以下代码用于列举带指定标签的Bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*初始化OSS账号信息*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*初始化网络等资源*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*列举带指定标签的Bucket*/
ListBucketsRequest request(BucketName);
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 {
/*异常处理*/
std::cout << "ListBuckets fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*释放网络等资源*/
ShutdownSdk();
return 0;
}
删除Bucket标签
以下代码用于删除Bucket标签:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*初始化OSS账号信息*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*初始化网络等资源*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*删除Bucket标签*/
DeleteBucketTaggingRequest request(BucketName);
auto outcome = client.DeleteBucketTagging(request);
if (outcome.isSuccess()) {
std::cout << " DeleteBucketTagging success " << std::endl;
}
else {
/*异常处理*/
std::cout << "DeleteBucketTagging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*释放网络等资源*/
ShutdownSdk();
return 0;
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论