You need to enable JavaScript to run this app.
导航
管理桶生命周期(Java SDK)
最近更新时间:2024.02.04 18:31:02首次发布时间:2023.01.19 14:37:15

桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。TOS 支持针对桶设置生命周期(Lifecycle)规则,自动删除过期的对象(Object)和未合并的分片数据,或将到期的对象转化为低频或归档存储类型,从而节约用户存储费用。本文介绍如何通过 TOS Java SDK 管理桶的生命周期规则。

设置生命周期规则

您可以通过 TOS Java SDK 的 putBucketLifecycle 接口设置指定桶的生命周期规则。

注意

  • 要为桶配置生命周期规则,您的账号必须具备 tos:PutLifecycleConfiguration 权限。具体操作请参见权限配置概述
  • 如果指定桶已配置过生命周期规则,再次执行此操作将覆盖原有配置。
  • 如果需要对指定桶生命周期规则进行追加或修改,可以先使用 Java SDK 的 getBucketLifecycle 接口获取桶当前的规则,接着对当前规则进行追加或修改,最后再调用putBucketLifecycle 更新规则。关于 getBucketLifecycle 接口的使用,请参照下文。
  • 关于生命周期规则的更多介绍,请参见桶生命周期规则

示例代码

以下代码展示如何设置桶的生命周期规则。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.comm.common.StatusType;
import com.volcengine.tos.comm.common.StorageClassType;
import com.volcengine.tos.model.bucket.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PutBucketLifecycleExample {
    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 = "your bucket name";

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

        try{
            List<LifecycleRule> rules = new ArrayList<>();
            // rule1,对象创建 70 天后过期
            LifecycleRule rule1 = new LifecycleRule()
                    .setId("rule1")
                    .setPrefix("test1/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setExpiration(new Expiration().setDays(70));
            rules.add(rule1);
            // rule2,对象于指定时间后过期
            Date expirationDate = Date.from(LocalDateTime.of(2023, 12, 31, 0, 0, 0, 0).toInstant(ZoneOffset.UTC));
            LifecycleRule rule2 = new LifecycleRule()
                    .setId("rule2")
                    .setPrefix("test2/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setExpiration(new Expiration().setDate(expirationDate));
            rules.add(rule2);
            // rule3,使用分片上传接口,已上传但未合并的分片的过期规则
            AbortInCompleteMultipartUpload abortRule = new AbortInCompleteMultipartUpload().setDaysAfterInitiation(10);
            LifecycleRule rule3 = new LifecycleRule()
                    .setId("rule3")
                    .setPrefix("test3/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setAbortInCompleteMultipartUpload(abortRule);
            rules.add(rule3);
            // rule4,设置对象的转化规则。指定对象在 30 天后转化为低频存储
            Transition transition = new Transition().setDays(30).setStorageClass(StorageClassType.STORAGE_CLASS_IA);
            List<Transition> transitions = new ArrayList<>();
            transitions.add(transition);
            LifecycleRule rule4 = new LifecycleRule()
                    .setId("rule4")
                    .setPrefix("test4/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setTransitions(transitions);
            rules.add(rule4);
            // rule5,设置匹配标签,与 rule4 不同在于,除了匹配前缀外需对象被设置了对应标签才能匹配此规则
            Tag tag = new Tag().setKey("key1").setValue("value1");
            List<Tag> tags = new ArrayList<>();
            tags.add(tag);
            LifecycleRule rule5 = new LifecycleRule()
                    .setId("rule5")
                    .setPrefix("test5/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setTransitions(transitions)
                    .setTags(tags);
            rules.add(rule5);
            // rule6,针对开启多版本对象的桶
            // 设置最新版本在70天后过期
            // 历史版本在 30 天后转化为低频存储
            // 历史版本在 40 天后过期
            NoncurrentVersionTransition noncurrentVersionTransition = new NoncurrentVersionTransition().setNoncurrentDays(30)
                    .setStorageClass(StorageClassType.STORAGE_CLASS_IA);
            List<NoncurrentVersionTransition> noncurrentVersionTransitions = new ArrayList<>();
            noncurrentVersionTransitions.add(noncurrentVersionTransition);
            NoncurrentVersionExpiration noncurrentVersionExpiration = new NoncurrentVersionExpiration().setNoncurrentDays(40);
            LifecycleRule rule6 = new LifecycleRule()
                    .setId("rule6")
                    .setPrefix("test6/")
                    .setStatus(StatusType.STATUS_ENABLED)
                    .setExpiration(new Expiration().setDays(70))
                    .setNoncurrentVersionExpiration(noncurrentVersionExpiration)
                    .setNoncurrentVersionTransitions(noncurrentVersionTransitions);
            rules.add(rule6);

            PutBucketLifecycleInput input = new PutBucketLifecycleInput().setBucket(bucketName).setRules(rules);
            PutBucketLifecycleOutput output = tos.putBucketLifecycle(input);
            System.out.println("putBucketLifecycle succeed");
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("putBucketLifecycle failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("putBucketLifecycle 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("putBucketLifecycle failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

获取生命周期规则

您可以通过 TOS Java SDK 的 getBucketLifecycle 接口获取指定桶当前配置的生命周期规则。

注意

  • 要为桶设置生命周期规则,您的账号必须具备 tos:GetLifecycleConfiguration 权限。具体操作请参见权限配置概述
  • 关于生命周期规则的更多介绍,请参见桶生命周期规则

示例代码

以下代码展示如何获取桶的生命周期规则。

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.GetBucketLifecycleInput;
import com.volcengine.tos.model.bucket.GetBucketLifecycleOutput;
import com.volcengine.tos.model.bucket.LifecycleRule;

public class GetBucketLifecycleExample {
    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 = "your bucket name";

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

        try{
            GetBucketLifecycleInput input = new GetBucketLifecycleInput().setBucket(bucketName);
            GetBucketLifecycleOutput output = tos.getBucketLifecycle(input);
            System.out.println("getBucketLifecycle succeed");
            if (output.getRules() != null) {
                System.out.println("this bucket has " + output.getRules().size() + " lifecycle rules");
                for (int i = 0; i < output.getRules().size(); i++){
                    LifecycleRule rule = output.getRules().get(i);
                    System.out.println("No." + (i+1) + " rule is " + rule.toString());
                }
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("getBucketLifecycle failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("getBucketLifecycle 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("getBucketLifecycle failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

删除生命周期规则

您可以通过 TOS Java SDK 的 deleteBucketLifecycle 接口删除指定桶当前配置的生命周期规则。

注意

  • 要删除桶的生命周期规则,您的账号必须具备 tos:PutLifecycleConfiguration 权限,具体操作请参见权限配置概述
  • 关于生命周期规则的更多介绍,请参见桶生命周期规则

示例代码

以下代码展示如何删除桶的生命周期规则。

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.DeleteBucketLifecycleInput;
import com.volcengine.tos.model.bucket.DeleteBucketLifecycleOutput;

public class DeleteBucketLifecycleExample {
    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 = "your bucket name";

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

        try{
            DeleteBucketLifecycleInput input = new DeleteBucketLifecycleInput().setBucket(bucketName);
            DeleteBucketLifecycleOutput output = tos.deleteBucketLifecycle(input);
            System.out.println("deleteBucketLifecycle succeed, " + output);
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
            System.out.println("deleteBucketLifecycle failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
            System.out.println("deleteBucketLifecycle 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("deleteBucketLifecycle failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

相关文档

关于生命周期规则的更多信息,请参见设置生命周期规则