You need to enable JavaScript to run this app.
导航
管理对象标签(Java SDK)
最近更新时间:2024.02.04 18:31:03首次发布时间:2023.01.19 14:37:16

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

注意事项

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

设置对象标签

以下代码展示如何使用 SDK 的 putObjectTagging 接口设置桶 bucket-exampleobject-example 对象的标签。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.bucket.Tag;
import com.volcengine.tos.model.object.PutObjectTaggingInput;
import com.volcengine.tos.model.object.PutObjectTaggingOutput;
import com.volcengine.tos.model.object.TagSet;

import java.util.ArrayList;
import java.util.List;

public class PutObjectTaggingExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        String objectKey = "example_dir/example_object.txt";

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);

        try{
            PutObjectTaggingInput input = new PutObjectTaggingInput().setBucket(bucketName).setKey(objectKey);
            Tag tag1 = new Tag().setKey("tagA").setValue("valueA");
            Tag tag2 = new Tag().setKey("tagB").setValue("valueB");
            List<Tag> tagList = new ArrayList<>();
            tagList.add(tag1);
            tagList.add(tag2);
            TagSet tagSet = new TagSet();
            tagSet.setTags(tagList);
            input.setTagSet(tagSet);
            PutObjectTaggingOutput output = tos.putObjectTagging(input);
            System.out.println("putObjectTagging succeed");
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("putObjectTagging failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("putObjectTagging failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("putObjectTagging failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

获取对象标签

设置对象标签后,您可以获取对象的标签信息。
以下代码展示如何使用 SDK 的 getObjectTagging 接口获取桶 bucket-exampleobject-example 对象的标签信息。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.bucket.Tag;
import com.volcengine.tos.model.object.GetObjectTaggingInput;
import com.volcengine.tos.model.object.GetObjectTaggingOutput;

public class GetObjectTaggingExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        String objectKey = "example_dir/example_object.txt";

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);

        try{
            GetObjectTaggingInput input = new GetObjectTaggingInput().setBucket(bucketName).setKey(objectKey);
            GetObjectTaggingOutput output = tos.getObjectTagging(input);
            System.out.println("getObjectTagging succeed");
            if (output.getTagSet() != null && output.getTagSet().getTags() != null) {
                System.out.println("this object has " + output.getTagSet().getTags().size() + " tags");
                for (int i = 0; i < output.getTagSet().getTags().size(); i++){
                    Tag tag = output.getTagSet().getTags().get(i);
                    System.out.println("No." + (i+1) + " tag is " + tag);
                }
            } else {
                System.out.println("this object does not have tags.");
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("getObjectTagging failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("getObjectTagging failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("getObjectTagging failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

删除对象标签

以下代码展示如何使用 SDK 的 deleteObjectTagging 接口删除桶 bucket-exampleobject-example 对象的标签信息。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.DeleteObjectTaggingInput;
import com.volcengine.tos.model.object.DeleteObjectTaggingOutput;

public class DeleteObjectTaggingExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY");

        String bucketName = "bucket-example";
        String objectKey = "example_dir/example_object.txt";

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);

        try{
            DeleteObjectTaggingInput input = new DeleteObjectTaggingInput().setBucket(bucketName).setKey(objectKey);
            DeleteObjectTaggingOutput output = tos.deleteObjectTagging(input);
            System.out.println("deleteObjectTagging succeed.");
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("deleteObjectTagging failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("deleteObjectTagging failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里
            System.out.println("deleteObjectTagging failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

相关文档

关于对象标签的更多信息,请参见 PutObjectTagging