メニュー
Amazon Simple Storage Service
開発者ガイド (API バージョン 2006-03-01)

AWS SDK for Java を使用したオブジェクトのライフサイクルの管理

AWS SDK for Java を使用して、バケットのライフサイクル設定を管理できます。ライフサイクル設定の管理の詳細については、「オブジェクトのライフサイクル管理」を参照してください。

注記

バケットにライフサイクル設定を追加すると、Amazon S3 でバケットの現在のライフサイクル設定 (ある場合) が置き換えられます。設定を更新するには、設定を取得して必要な変更を加え、変更済みのライフサイクル設定をバケットに追加します。

次の例では、AWS SDK for Java を使用してバケットのライフサイクル設定を追加、更新、削除する方法を示します。この例では、次のような処理を実行します。

  • バケットへのライフサイクル設定の追加

  • ライフサイクル設定を取得し、別のルールを追加して設定を更新します。

  • バケットに変更済みのライフサイクル設定を追加します。Amazon S3 によって既存の設定が置き換えられます。

  • 再び設定を取得し、ルールを出力してルール数が適切であることを確認します。

  • ライフサイクル設定を削除し、削除済みであることを確認するために、設定の取得を再度試行します。

有効な例を作成してテストする方法については、「Amazon S3 Java コード例のテスト」を参照してください。

import java.io.IOException; import java.util.Arrays; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; 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 void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; // Create a rule to archive objects with the "glacierobjects/" prefix to Glacier immediately. 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); // 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") .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); // Add the rules to a new BucketLifecycleConfiguration. BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration() .withRules(Arrays.asList(rule1, rule2)); try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Save the configuration. s3Client.setBucketLifecycleConfiguration(bucketName, configuration); // Retrieve the configuration. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); // Add a new rule with both a prefix predicate and a tag predicate. 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)); // Save the configuration. s3Client.setBucketLifecycleConfiguration(bucketName, configuration); // Retrieve the configuration. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); // Verify that the configuration now has three rules. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); System.out.println("Expected # of rules = 3; found: " + configuration.getRules().size()); // Delete the configuration. s3Client.deleteBucketLifecycleConfiguration(bucketName); // Verify that the configuration has been deleted by attempting to retrieve it. configuration = s3Client.getBucketLifecycleConfiguration(bucketName); String s = (configuration == null) ? "No configuration found." : "Configuration found."; System.out.println(s); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }