Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用AWS SDK for Java管理对象生命周期

您可以使用AWS SDK for Java管理存储桶上的生命周期配置。有关管理生命周期配置的更多信息,请参阅 对象生命周期管理

本主题中的示例代码执行以下操作:

  • 添加包含以下两个规则的生命周期配置:

    • 应用于带 glacierobjects/ 键名称前缀的对象的规则。该规则指定了指示 Amazon S3 将这些对象转换为 GLACIER 存储类别的转换操作。由于指定的天数为 0,因此对象立即变得有资格存档。

    • 应用于具有带标签键 archive 和值 true 的标签的对象的规则。该规则指定了两个转换操作,即指示 Amazon S3 在对象创建 30 天后将其转换为 STANDARD_IA (IA,适用于不常访问) 存储类别,然后在对象创建 365 天后将其转换为 GLACIER 存储类别。该规则还指定了过期操作,即指示 Amazon S3 在这些对象创建 3650 天后将其删除。

  • 检索生命周期配置。

  • 通过添加应用于带 YearlyDocuments/ 键名称前缀的对象的另一个规则来更新配置。此规则中的过期操作指示 Amazon S3 在这些对象创建 3650 天之后将其删除。

    注意

    向存储桶添加生命周期配置时,将替换任何现有的生命周期配置。要更新现有的生命周期配置,您必须首先检索现有的生命周期配置,进行更改,然后向存储桶添加已修订的生命周期配置。

例 Java 代码示例

下面的 Java 代码示例提供了一个完整的代码列表,用于为存储桶添加、更新和删除生命周期配置。您需要更新代码,并且需要提供代码可向其添加示例生命周期配置的存储桶名称。

有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

import java.io.IOException; import java.util.Arrays; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.BucketLifecycleConfiguration; import com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition; import com.amazonaws.services.s3.model.StorageClass; import com.amazonaws.services.s3.model.Tag; import com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator; import com.amazonaws.services.s3.model.lifecycle.LifecycleFilter; import com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate; import com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate; public class LifecycleConfiguration { public static String bucketName = "*** Provide bucket name ***"; public static AmazonS3Client s3Client; public static void main(String[] args) throws IOException { s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { BucketLifecycleConfiguration.Rule rule1 = new BucketLifecycleConfiguration.Rule() .withId("Archive immediately rule") .withFilter(new LifecycleFilter( new LifecyclePrefixPredicate("glacierobjects/"))) .addTransition(new Transition() .withDays(0) .withStorageClass(StorageClass.Glacier)) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule() .withId("Archive and then delete rule") .withFilter(new LifecycleFilter( new LifecycleTagPredicate(new Tag("archive", "true")))) .addTransition(new Transition() .withDays(30) .withStorageClass(StorageClass.StandardInfrequentAccess)) .addTransition(new Transition() .withDays(365) .withStorageClass(StorageClass.Glacier)) .withExpirationInDays(3650) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration() .withRules(Arrays.asList(rule1, rule2)); // Save configuration. s3Client.setBucketLifecycleConfiguration(bucketName, configuration); // Retrieve configuration. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); // Add a new rule. configuration.getRules().add( new BucketLifecycleConfiguration.Rule() .withId("NewRule") .withFilter(new LifecycleFilter( new LifecycleAndOperator(Arrays.asList( new LifecyclePrefixPredicate("YearlyDocuments/"), new LifecycleTagPredicate(new Tag("expire_after", "ten_years")))))) .withExpirationInDays(3650) .withStatus(BucketLifecycleConfiguration. ENABLED.toString()) ); // Save configuration. s3Client.setBucketLifecycleConfiguration(bucketName, configuration); // Retrieve configuration. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); // Verify there are now three rules. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); System.out.format("Expected # of rules = 3; found: %s\n", configuration.getRules().size()); System.out.println("Deleting lifecycle configuration. Next, we verify deletion."); // Delete configuration. s3Client.deleteBucketLifecycleConfiguration(bucketName); // Retrieve nonexistent configuration. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); String s = (configuration == null) ? "No configuration found." : "Configuration found."; System.out.println(s); } catch (AmazonS3Exception amazonS3Exception) { System.out.format("An Amazon S3 error occurred. Exception: %s", amazonS3Exception.toString()); } catch (Exception ex) { System.out.format("Exception: %s", ex.toString()); } } }