use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project aws-sdk-android by aws-amplify.
the class BucketLifecycleConfigurationIntegrationTest method testBucketLifecycle.
@Test
public void testBucketLifecycle() throws Exception {
// Check the bucket for its existing lifecycle config
BucketLifecycleConfiguration bucketLifecycleConfiguration = s3.getBucketLifecycleConfiguration(BUCKET_NAME);
assertNull(bucketLifecycleConfiguration);
// Apply a config
String ruleId = "abc";
Transition transition = new Transition();
transition.setDays(TRANSITION_TIME_IN_DAYS);
transition.setStorageClass(StorageClass.Glacier);
Rule rule1 = new Rule().withExpirationInDays(EXPIRATION_IN_DAYS).withId(ruleId).withPrefix("prefix").withStatus(BucketLifecycleConfiguration.ENABLED).withTransition(transition);
transition = new Transition();
transition.setDate(ServiceUtils.parseIso8601Date(TRANSITION_DATE));
transition.setStorageClass(StorageClass.Glacier);
Rule rule2 = new Rule().withExpirationDate(ServiceUtils.parseIso8601Date(EXPIRATION_DATE)).withTransition(transition).withPrefix("another").withStatus(BucketLifecycleConfiguration.DISABLED);
BucketLifecycleConfiguration config = new BucketLifecycleConfiguration().withRules(rule1, rule2);
s3.setBucketLifecycleConfiguration(BUCKET_NAME, config);
// Check reading it back
bucketLifecycleConfiguration = waitForBucketLifecycleConfiguration(BUCKET_NAME);
assertNotNull(bucketLifecycleConfiguration);
assertEquals(2, bucketLifecycleConfiguration.getRules().size());
boolean seen1 = false, seen2 = false;
for (Rule rule : bucketLifecycleConfiguration.getRules()) {
if (rule.getId().equals(ruleId)) {
seen1 = true;
assertEquals(BucketLifecycleConfiguration.ENABLED, rule.getStatus());
assertEquals("prefix", rule.getPrefix());
assertEquals(EXPIRATION_IN_DAYS, rule.getExpirationInDays());
assertEquals(TRANSITION_TIME_IN_DAYS, rule.getTransition().getDays());
assertEquals(StorageClass.Glacier, rule.getTransition().getStorageClass());
} else {
seen2 = true;
assertNotNull(rule.getId());
assertEquals(BucketLifecycleConfiguration.DISABLED, rule.getStatus());
assertEquals(ServiceUtils.formatIso8601Date(rule.getExpirationDate()), EXPIRATION_DATE);
assertEquals(ServiceUtils.formatIso8601Date(rule.getTransition().getDate()), TRANSITION_DATE);
assertEquals(StorageClass.Glacier, rule.getTransition().getStorageClass());
assertEquals("another", rule.getPrefix());
}
}
assertTrue(seen1);
assertTrue(seen2);
// Now put some objects and see if they have the right headers returned
String expiringKey = "prefixKey";
s3.putObject(BUCKET_NAME, expiringKey, file);
ObjectMetadata metadataExpriringKey = waitForObjectWithExpirationKeyExisit(BUCKET_NAME, expiringKey);
String nonExpiringKey = "anotherKey";
s3.putObject(BUCKET_NAME, nonExpiringKey, file);
ObjectMetadata metadataNonExpriringKey = waitForObjectWithNonExpirationKeyExisit(BUCKET_NAME, nonExpiringKey);
assertNotNull(metadataExpriringKey.getExpirationTime());
assertEquals(ruleId, metadataExpriringKey.getExpirationTimeRuleId());
assertNull(metadataNonExpriringKey.getExpirationTime());
assertNull(metadataNonExpriringKey.getExpirationTimeRuleId());
// There are several APIs that are affected by this header; test them
ObjectMetadata copyObjectMetadata = null;
s3.copyObject(new CopyObjectRequest(BUCKET_NAME, expiringKey, BUCKET_NAME, expiringKey + "2"));
copyObjectMetadata = waitForObjectWithExpirationKeyExisit(BUCKET_NAME, expiringKey + "2");
assertNotNull(copyObjectMetadata.getExpirationTime());
assertEquals(ruleId, copyObjectMetadata.getExpirationTimeRuleId());
s3.copyObject(new CopyObjectRequest(BUCKET_NAME, nonExpiringKey, BUCKET_NAME, nonExpiringKey + "2"));
copyObjectMetadata = waitForObjectWithNonExpirationKeyExisit(BUCKET_NAME, nonExpiringKey + "2");
assertNull(copyObjectMetadata.getExpirationTime());
assertNull(copyObjectMetadata.getExpirationTimeRuleId());
metadataExpriringKey = waitForObjectWithExpirationKeyExisit(BUCKET_NAME, expiringKey);
assertNotNull(metadataExpriringKey.getExpirationTime());
assertEquals(ruleId, metadataExpriringKey.getExpirationTimeRuleId());
metadataNonExpriringKey = waitForObjectWithNonExpirationKeyExisit(BUCKET_NAME, nonExpiringKey);
assertNull(metadataNonExpriringKey.getExpirationTime());
assertNull(metadataNonExpriringKey.getExpirationTimeRuleId());
// TODO: test multipart upload
}
use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project entrada by SIDN.
the class AmazonInitializer method enableBucketLifecycle.
private boolean enableBucketLifecycle(String location, String prefix, int exp, boolean optional) {
if (optional && !fileManager.supported(location)) {
// do nothing
return true;
}
Optional<S3Details> s3Loc = S3Details.from(location);
if (s3Loc.isPresent()) {
BucketLifecycleConfiguration cfg = amazonS3.getBucketLifecycleConfiguration(bucket);
if (cfg == null) {
// no config found, ceate a new config
cfg = new BucketLifecycleConfiguration().withRules(new ArrayList<>());
}
Optional<Rule> oldRule = cfg.getRules().stream().filter(r -> StringUtils.startsWithIgnoreCase(r.getId(), prefix)).findFirst();
if (!oldRule.isPresent()) {
// no rule found found create new
cfg.getRules().add(createExpirationRule(prefix + " after " + exp + " day(s)", s3Loc.get().getKey(), exp));
// Save the configuration.
amazonS3.setBucketLifecycleConfiguration(bucket, cfg);
return true;
} else {
// existing rule found, check if need to update
if (oldRule.get().getExpirationInDays() != exp) {
log.info("Lifecycle policy rule with prefix: '{}' has changed from {} to {}, creating new policy rule.", prefix, oldRule.get().getExpirationInDays(), exp);
// remove old rule and add new rule
cfg.getRules().remove(oldRule.get());
cfg.getRules().add(createExpirationRule(prefix + " after " + exp + " day(s)", s3Loc.get().getKey(), exp));
amazonS3.setBucketLifecycleConfiguration(bucket, cfg);
}
return true;
}
}
return false;
}
Aggregations