You need to enable JavaScript to run this app.
导航
管理对象标签(.NET SDK)
最近更新时间:2024.06.18 11:09:00首次发布时间:2024.06.18 11:07:49

TOS 支持为对象设置标签,并根据对象标签,完成特定对象的生命周期管理。本文介绍如何管理对象标签。

注意事项

  • 设置对象标签,您必须具备 tos:PutObjectTagging 权限,具体操作,请参见权限配置指南
  • 获取对象标签,您必须具备 tos:GetObjectTagging 权限,具体操作,请参见权限配置指南
  • 删除对象标签,您必须具备 tos:DeleteObjectTagging 权限,具体操作,请参见权限配置指南
  • 最多支持为每个对象添加 10 个标签,并且同一对象的 Key 不能重复,关于对象标签的限制请参见对象标签限制说明

设置对象标签

TOS 支持使用对象标签对桶中文件进行分类,您可以针对相同的对象标签设置生命周期规则。以下代码展示如何设置对象标签。

using System;
using TOS;
using TOS.Error;
using TOS.Model;

namespace ConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");
            var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");
            // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS
            // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
            var endpoint = "https://tos-cn-beijing.volces.com";
            var region = "cn-beijing";
            // 填写 BucketName
            var bucketName = "*** Provide your bucket name ***";
            // 填写对象名
            var objectKey = "*** Provide your object key ***";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();

            // 设置对象指定 Tag
            var tags = new []
            {
                new Tag
                {
                    Key = "key1",
                    Value = "value1"
                },
                new Tag
                {
                    Key = "key2",
                    Value = "value2"
                }
            };
            
            try
            {
                // 设置对象标签
                var putObjectTaggingInput = new PutObjectTaggingInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    TagSet = new TagSet{Tags = tags},
                    // 如果桶开启的多版本,通过设置 VersionID 来设置指定版本
                    VersionID = "",
                };

                var putObjectTaggingOutput = client.PutObjectTagging(putObjectTaggingInput);
                Console.WriteLine("PutObjectTagging succeeded, Request ID {0}", putObjectTaggingOutput.RequestID);
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("PutObjectTagging failed, Request ID {0}", ex.RequestID);
                Console.WriteLine("PutObjectTagging failed, Status Code {0}", ex.StatusCode);
                Console.WriteLine("PutObjectTagging failed, Response Error Code {0}", ex.Code);
                Console.WriteLine("PutObjectTagging failed, Response Error Message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("PutObjectTagging failed, Error Message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("PutObjectTagging failed, {0}", ex.Message);
            }
        }
    }
}

获取对象标签

设置对象标签后,您可以根据获取对象的标签信息。当桶开启版本控制时,TOS 默认返回对象当前版本的标签信息,您可以通过指定对象的版本来获取指定版本的标签信息。

using System;
using TOS;
using TOS.Error;
using TOS.Model;

namespace ConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");
            var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");
            // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS
            // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
            var endpoint = "https://tos-cn-beijing.volces.com";
            var region = "cn-beijing";
            // 填写 BucketName
            var bucketName = "*** Provide your bucket name ***";
            // 填写对象名
            var objectKey = "*** Provide your object key ***";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();
            
            try
            {
                // 获取对象标签
                var getObjectTaggingInput = new GetObjectTaggingInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    // 如果桶开启的多版本,通过设置 VersionID 来设置指定版本
                    VersionID = "",
                };

                var getObjectTaggingOutput = client.GetObjectTagging(getObjectTaggingInput);
                Console.WriteLine("GetObjectTagging succeeded, Request ID {0}", getObjectTaggingOutput.RequestID);
                foreach (var tag in getObjectTaggingOutput.TagSet.Tags)
                {
                    Console.WriteLine("Tag key: {0}, tag value: {1}", tag.Key, tag.Value);
                }
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("GetObjectTagging failed, Request ID {0}", ex.RequestID);
                Console.WriteLine("GetObjectTagging failed, Status Code {0}", ex.StatusCode);
                Console.WriteLine("GetObjectTagging failed, Response Error Code {0}", ex.Code);
                Console.WriteLine("GetObjectTagging failed, Response Error Message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("GetObjectTagging failed, Error Message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetObjectTagging failed, {0}", ex.Message);
            }
        }
    }
}

删除对象标签

您可以根据需要删除不需要的标签信息。当桶开启多版本时,TOS 默认只删除对象当前版本的标签信息,您可以通过指定对象的版本号来删除指定版本的标签信息。

using System;
using TOS;
using TOS.Error;
using TOS.Model;

namespace ConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");
            var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");
            // endpoint 若没有指定HTTP协议(HTTP/HTTPS),默认使用 HTTPS
            // Bucket 的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
            var endpoint = "https://tos-cn-beijing.volces.com";
            var region = "cn-beijing";
            // 填写 BucketName
            var bucketName = "*** Provide your bucket name ***";
            // 填写对象名
            var objectKey = "*** Provide your object key ***";

            // 创建TOSClient实例
            var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();
            
            try
            {
                // 删除对象标签
                var deleteObjectTaggingInput = new DeleteObjectTaggingInput()
                {
                    Bucket = bucketName,
                    Key = objectKey,
                    // 如果桶开启的多版本,通过设置 VersionID 来设置指定版本
                    VersionID = "",
                };

                var deleteObjectTaggingOutput = client.DeleteObjectTagging(deleteObjectTaggingInput);
                Console.WriteLine("DeleteObjectTagging succeeded, Request ID {0}", deleteObjectTaggingOutput.RequestID);
            }
            catch (TosServerException ex)
            {
                Console.WriteLine("DeleteObjectTagging failed, Request ID {0}", ex.RequestID);
                Console.WriteLine("DeleteObjectTagging failed, Status Code {0}", ex.StatusCode);
                Console.WriteLine("DeleteObjectTagging failed, Response Error Code {0}", ex.Code);
                Console.WriteLine("DeleteObjectTagging failed, Response Error Message {0}", ex.Message);
            }
            catch (TosClientException ex)
            {
                Console.WriteLine("DeleteObjectTagging failed, Error Message {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("DeleteObjectTagging failed, {0}", ex.Message);
            }
        }
    }
}

相关文档

关于对象标签的 API 文档,请参见 PutObjectTagging