文档中心 > 生命周期
生命周期

最近更新时间:2022-07-25

本文介绍如何管理生命周期。


生命周期规则介绍

UOS支持设置生命周期(Lifecycle)规则,自动删除过期的文件和碎片,或将到期的文件转储为低频或归档存储类型,从而节省存储费用。每条规则包含:

  • 规则ID。用于标识一条规则,同一桶内规则ID不能重复。

  • 策略。有以下两种设置方式。同一桶内仅支持一种设置方式。

    • 按前缀匹配。此种方式允许创建多条规则,前缀不能重复。

    • 配置到整个桶。此种方式只能创建一条规则。

  • 过期时间。有两种指定方式:

    • 指定一个过期天数N,文件会在其最近更新时间点的N天后过期。

    • 指定一个过期时间点,最近更新时间在该时间点之前的文件全部过期。

  • 是否生效。

通过uploadPart方法上传的分片也支持设置生命周期规则。文件最后修改时间以初始化分片上传事件的时间为准。


设置生命周期规则

以下代码用于设置生命周期规则:

public void testBucketLifecycle(){

    // Create a rule to archive objects with the "glacierobjects/" prefix to Glacier immediately.

        BucketLifecycleConfiguration.Rule rule1 = new BucketLifecycleConfiguration.Rule()

                .withId("Archive immediately rule")

                .withPrefix("documents/")

                .addTransition(new Transition().withDays(0).withStorageClass(StorageClass.Glacier))

                .withStatus(BucketLifecycleConfiguration.ENABLED);


   // Create a rule to transition objects to the Standard-Infrequent Access storage class

   // after 30 days, then to Glacier after 365 days. Amazon S3 will delete the objects after 3650 days.

        // The rule applies to all objects with the tag "archive" set to "true".

        BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule()

                .withId("Archive and then delete rule")

                .withPrefix("documents/")

                .addTransition(new Transition().withDays(30).withStorageClass(StorageClass.StandardInfrequentAccess))

                .addTransition(new Transition().withDays(365).withStorageClass(StorageClass.Glacier))

                .withExpirationInDays(3650)

                .withStatus(BucketLifecycleConfiguration.ENABLED);


        // Add the rules to a new BucketLifecycleConfiguration.

        BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration()

                .withRules(Arrays.asList(rule1, rule2));


        try {

            // Save the configuration.

            s3.setBucketLifecycleConfiguration(bucketName, configuration);


            // Retrieve the configuration.

            configuration = s3.getBucketLifecycleConfiguration(bucketName);


            // Add a new rule with both a prefix predicate and a tag predicate.

            configuration.getRules().add(new BucketLifecycleConfiguration.Rule().withId("NewRule")

                    .withExpirationInDays(3650)

                    .withStatus(BucketLifecycleConfiguration.ENABLED));


            // Save the configuration.

            s3.setBucketLifecycleConfiguration(bucketName, configuration);


            // Retrieve the configuration.

            configuration = s3.getBucketLifecycleConfiguration(bucketName);


            // Verify that the configuration now has three rules.

            configuration = s3.getBucketLifecycleConfiguration(bucketName);

            System.out.println("Expected # of rules = 3; found: " + configuration.getRules().size());


            // Delete the configuration.

            s3.deleteBucketLifecycleConfiguration(bucketName);


            // Verify that the configuration has been deleted by attempting to retrieve it.

            configuration = s3.getBucketLifecycleConfiguration(bucketName);

            String s = (configuration == null) ? "No configuration found." : "Configuration found.";

            System.out.println(s);

        } catch (AmazonServiceException e) {

            System.err.println(e.getErrorMessage());

        }


    }