您可以通过存储空间(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标签:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OSSClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    //设置Bucket标签。
    var setRequest = new SetBucketTaggingRequest(bucketName);

    var tag1 = new Tag
    {
        Key = "project",
        Value = "projectone"
    };

    var tag2 = new Tag
    {
        Key = "user",
        Value = "jsmith"
    };

    setRequest.AddTag(tag1);
    setRequest.AddTag(tag2);    
    client.SetBucketTagging(setRequest);
    Console.WriteLine("Set bucket:{0} Tagging succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

有关设置Bucket标签的详情请参见PutBucketTags

获取Bucket标签

以下代码用于获取Bucket标签:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 查看Bucket标签。
    var result = client.GetBucketTagging(bucketName);
    Console.WriteLine("Get bucket:{0} tagging succeeded ", bucketName);

    for (var i = 0; i < result.Tags.Count; i++) {
        Console.WriteLine("bucket tagging key: {0}; bucket tagging value: {1}", result.Tags[i].Key, result.Tags[i].Value);
    }
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

有关获取Bucket标签的详情请参见GetBucketTags

列举带指定标签的Bucket

以下代码用于列举带指定标签的Bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 列举带指定标签的Bucket。
    var tag = new Tag
    {
        Key = "tag",
        Value = "value"
    };
    var listRequest = new ListBucketsRequest();
    listRequest.Tag = tag;
    var result = client.ListBuckets(listRequest);
    Console.WriteLine("list bucket:{0} succeeded ", bucketName);  
    foreach (var bucket in result.Buckets) {
        Console.WriteLine("bucket name: {0}", bucket.Name);
    }
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

删除Bucket标签

以下代码用于删除Bucket标签:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OSSClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 删除Bucket标签。
    client.DeleteBucketTagging(bucketName);
    Console.WriteLine("Delete bucket:{0} Tagging succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

有关删除Bucket标签详情请参见DeleteBucketTags