Search in sources :

Example 31 with Rule

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
}
Also used : BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Transition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.junit.Test)

Example 32 with Rule

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;
}
Also used : BucketNameUtils(com.amazonaws.services.s3.internal.BucketNameUtils) LifecyclePrefixPredicate(com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate) SSEAlgorithm(com.amazonaws.services.s3.model.SSEAlgorithm) StringUtils(org.apache.commons.lang3.StringUtils) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) FileManager(nl.sidnlabs.entrada.file.FileManager) ServerSideEncryptionRule(com.amazonaws.services.s3.model.ServerSideEncryptionRule) GetBucketEncryptionResult(com.amazonaws.services.s3.model.GetBucketEncryptionResult) Qualifier(org.springframework.beans.factory.annotation.Qualifier) AmazonS3(com.amazonaws.services.s3.AmazonS3) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ApplicationException(nl.sidnlabs.entrada.exception.ApplicationException) SetBucketEncryptionRequest(com.amazonaws.services.s3.model.SetBucketEncryptionRequest) QueryEngine(nl.sidnlabs.entrada.engine.QueryEngine) ServerSideEncryptionConfiguration(com.amazonaws.services.s3.model.ServerSideEncryptionConfiguration) ServerSideEncryptionByDefault(com.amazonaws.services.s3.model.ServerSideEncryptionByDefault) FileManagerFactory(nl.sidnlabs.entrada.file.FileManagerFactory) Component(org.springframework.stereotype.Component) SetPublicAccessBlockRequest(com.amazonaws.services.s3.model.SetPublicAccessBlockRequest) S3Details(nl.sidnlabs.entrada.file.S3FileManagerImpl.S3Details) Log4j2(lombok.extern.log4j.Log4j2) Optional(java.util.Optional) LifecycleFilter(com.amazonaws.services.s3.model.lifecycle.LifecycleFilter) PublicAccessBlockConfiguration(com.amazonaws.services.s3.model.PublicAccessBlockConfiguration) BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) ArrayList(java.util.ArrayList) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) ServerSideEncryptionRule(com.amazonaws.services.s3.model.ServerSideEncryptionRule) S3Details(nl.sidnlabs.entrada.file.S3FileManagerImpl.S3Details)

Aggregations

Test (org.junit.Test)15 Rule (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule)13 CORSRule (com.amazonaws.services.s3.model.CORSRule)7 BucketLifecycleConfiguration (com.amazonaws.services.s3.model.BucketLifecycleConfiguration)6 LifecycleTagPredicate (com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate)6 LifecycleAndOperator (com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator)5 HttpResponse (com.amazonaws.http.HttpResponse)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 XmlWriter (com.amazonaws.services.s3.internal.XmlWriter)4 RedirectRule (com.amazonaws.services.s3.model.RedirectRule)4 ReplicationRule (com.amazonaws.services.s3.model.ReplicationRule)4 LifecycleFilter (com.amazonaws.services.s3.model.lifecycle.LifecycleFilter)4 LifecycleFilterPredicate (com.amazonaws.services.s3.model.lifecycle.LifecycleFilterPredicate)4 LifecyclePrefixPredicate (com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 SdkClientException (com.amazonaws.SdkClientException)3 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)3 Regions (com.amazonaws.regions.Regions)3 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)3 ReplicationDestinationConfig (com.amazonaws.services.s3.model.ReplicationDestinationConfig)3