Search in sources :

Example 1 with BucketLifecycleConfiguration

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

the class LifecycleConfiguration method setLifecycleConfig.

// snippet-start:[s3.java2.manage_lifecycle.main]
public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) {
    try {
        // Create a rule to archive objects with the "glacierobjects/" prefix to Amazon S3 Glacier.
        LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder().prefix("glacierobjects/").build();
        Transition transition = Transition.builder().storageClass(TransitionStorageClass.GLACIER).days(0).build();
        LifecycleRule rule1 = LifecycleRule.builder().id("Archive immediately rule").filter(ruleFilter).transitions(transition).status(ExpirationStatus.ENABLED).build();
        // Create a second rule.
        Transition transition2 = Transition.builder().storageClass(TransitionStorageClass.GLACIER).days(0).build();
        List<Transition> transitionList = new ArrayList<>();
        transitionList.add(transition2);
        LifecycleRuleFilter ruleFilter2 = LifecycleRuleFilter.builder().prefix("glacierobjects/").build();
        LifecycleRule rule2 = LifecycleRule.builder().id("Archive and then delete rule").filter(ruleFilter2).transitions(transitionList).status(ExpirationStatus.ENABLED).build();
        // Add the LifecycleRule objects to an ArrayList.
        ArrayList<LifecycleRule> ruleList = new ArrayList<>();
        ruleList.add(rule1);
        ruleList.add(rule2);
        BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder().rules(ruleList).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 : BucketLifecycleConfiguration(software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration) LifecycleRuleFilter(software.amazon.awssdk.services.s3.model.LifecycleRuleFilter) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Transition(software.amazon.awssdk.services.s3.model.Transition) ArrayList(java.util.ArrayList) LifecycleRule(software.amazon.awssdk.services.s3.model.LifecycleRule) PutBucketLifecycleConfigurationRequest(software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)

Example 2 with BucketLifecycleConfiguration

use of software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration 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)

Example 3 with BucketLifecycleConfiguration

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

the class LifecycleConfiguration method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_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();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) LifecycleAndOperator(com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator) LifecycleFilter(com.amazonaws.services.s3.model.lifecycle.LifecycleFilter) Regions(com.amazonaws.regions.Regions) BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) LifecyclePrefixPredicate(com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate) SdkClientException(com.amazonaws.SdkClientException) LifecycleTagPredicate(com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate) Transition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Tag(com.amazonaws.services.s3.model.Tag)

Aggregations

ArrayList (java.util.ArrayList)2 BucketLifecycleConfiguration (software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration)2 LifecycleRule (software.amazon.awssdk.services.s3.model.LifecycleRule)2 LifecycleRuleFilter (software.amazon.awssdk.services.s3.model.LifecycleRuleFilter)2 PutBucketLifecycleConfigurationRequest (software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest)2 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)2 Transition (software.amazon.awssdk.services.s3.model.Transition)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 BucketLifecycleConfiguration (com.amazonaws.services.s3.model.BucketLifecycleConfiguration)1 Transition (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition)1 Tag (com.amazonaws.services.s3.model.Tag)1 LifecycleAndOperator (com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator)1 LifecycleFilter (com.amazonaws.services.s3.model.lifecycle.LifecycleFilter)1 LifecyclePrefixPredicate (com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate)1 LifecycleTagPredicate (com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate)1 GetBucketLifecycleConfigurationRequest (software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest)1