Search in sources :

Example 1 with GetBucketLifecycleConfigurationRequest

use of software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest in project aws-doc-sdk-examples by awsdocs.

the class LifecycleConfiguration method getLifecycleConfig.

// Retrieve the configuration and add a new rule.
public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId) {
    try {
        GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest.builder().bucket(bucketName).expectedBucketOwner(accountId).build();
        GetBucketLifecycleConfigurationResponse response = s3.getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest);
        // Create a new List.
        List<LifecycleRule> newList = new ArrayList<>();
        List<LifecycleRule> rules = response.rules();
        for (LifecycleRule rule : rules) {
            newList.add(rule);
        }
        // Add a new rule with both a prefix predicate and a tag predicate.
        LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder().prefix("YearlyDocuments/").build();
        Transition transition = Transition.builder().storageClass(TransitionStorageClass.GLACIER).days(3650).build();
        LifecycleRule rule1 = LifecycleRule.builder().id("NewRule").filter(ruleFilter).transitions(transition).status(ExpirationStatus.ENABLED).build();
        // Add the new rule to the list.
        newList.add(rule1);
        BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder().rules(newList).build();
        PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest.builder().bucket(bucketName).lifecycleConfiguration(lifecycleConfiguration).expectedBucketOwner(accountId).build();
        s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest) BucketLifecycleConfiguration(software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration) LifecycleRuleFilter(software.amazon.awssdk.services.s3.model.LifecycleRuleFilter) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Transition(software.amazon.awssdk.services.s3.model.Transition) GetBucketLifecycleConfigurationResponse(software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse) LifecycleRule(software.amazon.awssdk.services.s3.model.LifecycleRule) PutBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)

Aggregations

ArrayList (java.util.ArrayList)1 BucketLifecycleConfiguration (software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration)1 GetBucketLifecycleConfigurationRequest (software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest)1 GetBucketLifecycleConfigurationResponse (software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse)1 LifecycleRule (software.amazon.awssdk.services.s3.model.LifecycleRule)1 LifecycleRuleFilter (software.amazon.awssdk.services.s3.model.LifecycleRuleFilter)1 PutBucketLifecycleConfigurationRequest (software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1 Transition (software.amazon.awssdk.services.s3.model.Transition)1