Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/java_examples/S3Examples/LifecycleConfiguration.java
4084 views
1
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
3
4
import java.io.IOException;
5
import java.util.Arrays;
6
7
import com.amazonaws.AmazonServiceException;
8
import com.amazonaws.SdkClientException;
9
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
10
import com.amazonaws.services.s3.AmazonS3;
11
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
12
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration;
13
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition;
14
import com.amazonaws.services.s3.model.StorageClass;
15
import com.amazonaws.services.s3.model.Tag;
16
import com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator;
17
import com.amazonaws.services.s3.model.lifecycle.LifecycleFilter;
18
import com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate;
19
import com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate;
20
21
public class LifecycleConfiguration {
22
23
public static void main(String[] args) throws IOException {
24
String clientRegion = "*** Client region ***";
25
String bucketName = "*** Bucket name ***";
26
27
// Create a rule to archive objects with the "glacierobjects/" prefix to Glacier immediately.
28
BucketLifecycleConfiguration.Rule rule1 = new BucketLifecycleConfiguration.Rule()
29
.withId("Archive immediately rule")
30
.withFilter(new LifecycleFilter(new LifecyclePrefixPredicate("glacierobjects/")))
31
.addTransition(new Transition().withDays(0).withStorageClass(StorageClass.Glacier))
32
.withStatus(BucketLifecycleConfiguration.ENABLED);
33
34
// Create a rule to transition objects to the Standard-Infrequent Access storage class
35
// after 30 days, then to Glacier after 365 days. Amazon S3 will delete the objects after 3650 days.
36
// The rule applies to all objects with the tag "archive" set to "true".
37
BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule()
38
.withId("Archive and then delete rule")
39
.withFilter(new LifecycleFilter(new LifecycleTagPredicate(new Tag("archive", "true"))))
40
.addTransition(new Transition().withDays(30).withStorageClass(StorageClass.StandardInfrequentAccess))
41
.addTransition(new Transition().withDays(365).withStorageClass(StorageClass.Glacier))
42
.withExpirationInDays(3650)
43
.withStatus(BucketLifecycleConfiguration.ENABLED);
44
45
// Add the rules to a new BucketLifecycleConfiguration.
46
BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration()
47
.withRules(Arrays.asList(rule1, rule2));
48
49
try {
50
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
51
.withCredentials(new ProfileCredentialsProvider())
52
.withRegion(clientRegion)
53
.build();
54
55
// Save the configuration.
56
s3Client.setBucketLifecycleConfiguration(bucketName, configuration);
57
58
// Retrieve the configuration.
59
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
60
61
// Add a new rule with both a prefix predicate and a tag predicate.
62
configuration.getRules().add(new BucketLifecycleConfiguration.Rule().withId("NewRule")
63
.withFilter(new LifecycleFilter(new LifecycleAndOperator(
64
Arrays.asList(new LifecyclePrefixPredicate("YearlyDocuments/"),
65
new LifecycleTagPredicate(new Tag("expire_after", "ten_years"))))))
66
.withExpirationInDays(3650)
67
.withStatus(BucketLifecycleConfiguration.ENABLED));
68
69
// Save the configuration.
70
s3Client.setBucketLifecycleConfiguration(bucketName, configuration);
71
72
// Retrieve the configuration.
73
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
74
75
// Verify that the configuration now has three rules.
76
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
77
System.out.println("Expected # of rules = 3; found: " + configuration.getRules().size());
78
79
// Delete the configuration.
80
s3Client.deleteBucketLifecycleConfiguration(bucketName);
81
82
// Verify that the configuration has been deleted by attempting to retrieve it.
83
configuration = s3Client.getBucketLifecycleConfiguration(bucketName);
84
String s = (configuration == null) ? "No configuration found." : "Configuration found.";
85
System.out.println(s);
86
}
87
catch(AmazonServiceException e) {
88
// The call was transmitted successfully, but Amazon S3 couldn't process
89
// it, so it returned an error response.
90
e.printStackTrace();
91
}
92
catch(SdkClientException e) {
93
// Amazon S3 couldn't be contacted for a response, or the client
94
// couldn't parse the response from Amazon S3.
95
e.printStackTrace();
96
}
97
}
98
}
99