Search in sources :

Example 21 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project nexus-public by sonatype.

the class BucketManager method makeLifecycleConfiguration.

private BucketLifecycleConfiguration makeLifecycleConfiguration(final BucketLifecycleConfiguration existing, final BlobStoreConfiguration blobStoreConfiguration) {
    String blobStoreName = blobStoreConfiguration.getName();
    String bucketPrefix = getBucketPrefix(blobStoreConfiguration);
    int expirationInDays = getConfiguredExpirationInDays(blobStoreConfiguration);
    LifecycleFilterPredicate filterPredicate;
    if (bucketPrefix.isEmpty()) {
        filterPredicate = new LifecycleTagPredicate(S3BlobStore.DELETED_TAG);
    } else {
        filterPredicate = new LifecycleAndOperator(asList(new LifecyclePrefixPredicate(bucketPrefix), new LifecycleTagPredicate(S3BlobStore.DELETED_TAG)));
    }
    BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule().withId(LIFECYCLE_EXPIRATION_RULE_ID_PREFIX + blobStoreName).withFilter(new LifecycleFilter(filterPredicate)).withExpirationInDays(expirationInDays).withStatus(BucketLifecycleConfiguration.ENABLED);
    BucketLifecycleConfiguration newConfiguration = null;
    if (existing != null && !existing.getRules().isEmpty()) {
        List<Rule> rules = nonBlobstoreRules(existing, blobStoreName);
        if (expirationInDays > 0) {
            rules.add(rule);
        }
        if (!rules.isEmpty()) {
            existing.setRules(rules);
            newConfiguration = existing;
        }
    } else {
        if (expirationInDays > 0) {
            newConfiguration = new BucketLifecycleConfiguration().withRules(rule);
        }
    }
    return newConfiguration;
}
Also used : BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) LifecycleAndOperator(com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator) LifecyclePrefixPredicate(com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate) LifecycleFilter(com.amazonaws.services.s3.model.lifecycle.LifecycleFilter) LifecycleTagPredicate(com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate) LifecycleFilterPredicate(com.amazonaws.services.s3.model.lifecycle.LifecycleFilterPredicate) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule)

Example 22 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project aws-sdk-android by aws-amplify.

the class BucketWebsiteIntegrationTest method testBucketWebsites.

/**
 * Tests that we can get, set, and delete a bucket's website configuration.
 */
@Test
public void testBucketWebsites() throws Exception {
    // create a test bucket
    s3.createBucket(bucketName);
    // get website bucketWebsiteConfiguration for new bucket
    BucketWebsiteConfiguration bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    System.out.println("bucketWebsiteConfiguration: " + bucketWebsiteConfiguration);
    // set website configuration
    bucketWebsiteConfiguration = new BucketWebsiteConfiguration(INDEX_DOCUMENT_SUFFIX, ERROR_DOCUMENT);
    List<RoutingRule> rules = new LinkedList<RoutingRule>();
    RoutingRule rule = new RoutingRule();
    rule.setCondition(new RoutingRuleCondition().withKeyPrefixEquals(PREFIX).withHttpErrorCodeReturnedEquals(ERROR_CODE));
    rule.setRedirect(new RedirectRule().withProtocol(PROTOCOL).withHostName(HOST_NAME).withHttpRedirectCode(REDIRECT_CODE).withReplaceKeyPrefixWith(NEW_PREFIX));
    rules.add(rule);
    rule = new RoutingRule();
    rule.setRedirect(new RedirectRule().withHostName(HOST_NAME));
    rules.add(rule);
    rule = new RoutingRule();
    rule.setCondition(new RoutingRuleCondition().withHttpErrorCodeReturnedEquals(ERROR_CODE));
    rule.setRedirect(new RedirectRule().withReplaceKeyWith(NEW_KEY));
    rules.add(rule);
    bucketWebsiteConfiguration.setRoutingRules(rules);
    s3.setBucketWebsiteConfiguration(bucketName, bucketWebsiteConfiguration);
    // get again
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertEquals(INDEX_DOCUMENT_SUFFIX, bucketWebsiteConfiguration.getIndexDocumentSuffix());
    assertEquals(ERROR_DOCUMENT, bucketWebsiteConfiguration.getErrorDocument());
    assertEquals(3, bucketWebsiteConfiguration.getRoutingRules().size());
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getCondition().getKeyPrefixEquals(), PREFIX);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getCondition().getHttpErrorCodeReturnedEquals(), ERROR_CODE);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getprotocol(), PROTOCOL);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getHostName(), HOST_NAME);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getHttpRedirectCode(), REDIRECT_CODE);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getReplaceKeyPrefixWith(), NEW_PREFIX);
    assertNull(bucketWebsiteConfiguration.getRoutingRules().get(1).getCondition());
    assertEquals(HOST_NAME, bucketWebsiteConfiguration.getRoutingRules().get(1).getRedirect().getHostName());
    assertEquals("", bucketWebsiteConfiguration.getRoutingRules().get(2).getCondition().getKeyPrefixEquals());
    assertEquals(NEW_KEY, bucketWebsiteConfiguration.getRoutingRules().get(2).getRedirect().getReplaceKeyWith());
    // Test RedirectAllRequestsTo
    bucketWebsiteConfiguration = new BucketWebsiteConfiguration();
    bucketWebsiteConfiguration.setRedirectAllRequestsTo(new RedirectRule().withHostName(HOST_NAME).withProtocol(PROTOCOL));
    s3.setBucketWebsiteConfiguration(bucketName, bucketWebsiteConfiguration);
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertNotNull(bucketWebsiteConfiguration.getRedirectAllRequestsTo());
    assertEquals(HOST_NAME, bucketWebsiteConfiguration.getRedirectAllRequestsTo().getHostName());
    assertEquals(PROTOCOL, bucketWebsiteConfiguration.getRedirectAllRequestsTo().getprotocol());
    // delete
    s3.deleteBucketWebsiteConfiguration(bucketName);
    Thread.sleep(1000 * 60 * 1);
    // get again
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertNull(bucketWebsiteConfiguration);
}
Also used : RedirectRule(com.amazonaws.services.s3.model.RedirectRule) BucketWebsiteConfiguration(com.amazonaws.services.s3.model.BucketWebsiteConfiguration) RoutingRuleCondition(com.amazonaws.services.s3.model.RoutingRuleCondition) RoutingRule(com.amazonaws.services.s3.model.RoutingRule) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 23 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project aws-sdk-android by aws-amplify.

the class LifecycleVersioningIntegrationTest method testRoundTrip.

@Test
public void testRoundTrip() throws Exception {
    s3.setBucketLifecycleConfiguration(BUCKET_NAME, new BucketLifecycleConfiguration().withRules(new Rule().withPrefix("/quick").withId("Quick").withStatus("Enabled").withNoncurrentVersionTransition(new NoncurrentVersionTransition().withDays(0).withStorageClass(StorageClass.Glacier)).withNoncurrentVersionExpirationInDays(7), new Rule().withPrefix("/slow").withId("Slow").withStatus("Enabled").withNoncurrentVersionTransition(new NoncurrentVersionTransition().withDays(7).withStorageClass(StorageClass.Glacier)).withNoncurrentVersionExpirationInDays(60)));
    BucketLifecycleConfiguration result = waitForBucketLifecycleConfigurationCreated(BUCKET_NAME);
    Assert.assertEquals(2, result.getRules().size());
    Assert.assertEquals(0, result.getRules().get(0).getNoncurrentVersionTransition().getDays());
    Assert.assertEquals(StorageClass.Glacier, result.getRules().get(0).getNoncurrentVersionTransition().getStorageClass());
    Assert.assertEquals(7, result.getRules().get(0).getNoncurrentVersionExpirationInDays());
    Assert.assertEquals(7, result.getRules().get(1).getNoncurrentVersionTransition().getDays());
    Assert.assertEquals(StorageClass.Glacier, result.getRules().get(1).getNoncurrentVersionTransition().getStorageClass());
    Assert.assertEquals(60, result.getRules().get(1).getNoncurrentVersionExpirationInDays());
}
Also used : BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) NoncurrentVersionTransition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.NoncurrentVersionTransition) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) Test(org.junit.Test)

Example 24 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified {@link BucketLifecycleConfiguration} object to an
 * XML fragment that can be sent to Amazon S3.
 *
 * @param config The {@link BucketLifecycleConfiguration}
 */
/*
     * <LifecycleConfiguration> <Rule> <ID>logs-rule</ID>
     * <Status>Enabled</Status> <Transition> <Days>30</Days>
     * <StorageClass>GLACIER</StorageClass> </Transition> <Expiration>
     * <Days>365</Days> </Expiration> <NoncurrentVersionTransition>
     * <NoncurrentDays>7</NoncurrentDays> <StorageClass>GLACIER</StorageClass>
     * </NoncurrentVersionTransition> <NoncurrentVersionExpiration>
     * <NoncurrentDays>14</NoncurrentDays> </NoncurrentVersionExpiration>
     * <Filter> <!-- A filter can have only one of Prefix, Tag or And. -->
     * <Prefix>logs/</Prefix> <Tag> <Key>key1</Key> <Value>value1</Value> </Tag>
     * <And> <Prefix>logs/</Prefix> <Tag> <Key>key1</Key> <Value>value1</Value>
     * </Tag> <Tag> <Key>key1</Key> <Value>value1</Value> </Tag> </And>
     * </Filter> </Rule> <Rule> <ID>image-rule</ID> <Prefix>image/</Prefix>
     * <Status>Enabled</Status> <Transition>
     * <Date>2012-12-31T00:00:00.000Z</Date>
     * <StorageClass>GLACIER</StorageClass> </Transition> <Expiration>
     * <Date>2020-12-31T00:00:00.000Z</Date> </Expiration>
     * <AbortIncompleteMultipartUpload>
     * <DaysAfterInitiation>10</DaysAfterInitiation>
     * </AbortIncompleteMultipartUpload> </Rule> </LifecycleConfiguration>
     */
public byte[] convertToXmlByteArray(BucketLifecycleConfiguration config) throws AmazonClientException {
    final XmlWriter xml = new XmlWriter();
    xml.start("LifecycleConfiguration");
    for (final Rule rule : config.getRules()) {
        writeRule(xml, rule);
    }
    xml.end();
    return xml.getBytes();
}
Also used : CORSRule(com.amazonaws.services.s3.model.CORSRule) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule) ReplicationRule(com.amazonaws.services.s3.model.ReplicationRule) RoutingRule(com.amazonaws.services.s3.model.RoutingRule) RedirectRule(com.amazonaws.services.s3.model.RedirectRule) FilterRule(com.amazonaws.services.s3.model.FilterRule) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Example 25 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified website configuration into an XML byte array to
 * send to S3. Sample XML:
 * <WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
 * <IndexDocument> <Suffix>index.html</Suffix> </IndexDocument>
 * <ErrorDocument> <Key>404.html</Key> </ErrorDocument>
 * </WebsiteConfiguration>
 *
 * @param websiteConfiguration The configuration to convert.
 * @return The XML byte array representation.
 */
public byte[] convertToXmlByteArray(BucketWebsiteConfiguration websiteConfiguration) {
    final XmlWriter xml = new XmlWriter();
    xml.start("WebsiteConfiguration", "xmlns", Constants.XML_NAMESPACE);
    if (websiteConfiguration.getIndexDocumentSuffix() != null) {
        final XmlWriter indexDocumentElement = xml.start("IndexDocument");
        indexDocumentElement.start("Suffix").value(websiteConfiguration.getIndexDocumentSuffix()).end();
        indexDocumentElement.end();
    }
    if (websiteConfiguration.getErrorDocument() != null) {
        final XmlWriter errorDocumentElement = xml.start("ErrorDocument");
        errorDocumentElement.start("Key").value(websiteConfiguration.getErrorDocument()).end();
        errorDocumentElement.end();
    }
    final RedirectRule redirectAllRequestsTo = websiteConfiguration.getRedirectAllRequestsTo();
    if (redirectAllRequestsTo != null) {
        final XmlWriter redirectAllRequestsElement = xml.start("RedirectAllRequestsTo");
        if (redirectAllRequestsTo.getprotocol() != null) {
            xml.start("Protocol").value(redirectAllRequestsTo.getprotocol()).end();
        }
        if (redirectAllRequestsTo.getHostName() != null) {
            xml.start("HostName").value(redirectAllRequestsTo.getHostName()).end();
        }
        if (redirectAllRequestsTo.getReplaceKeyPrefixWith() != null) {
            xml.start("ReplaceKeyPrefixWith").value(redirectAllRequestsTo.getReplaceKeyPrefixWith()).end();
        }
        if (redirectAllRequestsTo.getReplaceKeyWith() != null) {
            xml.start("ReplaceKeyWith").value(redirectAllRequestsTo.getReplaceKeyWith()).end();
        }
        redirectAllRequestsElement.end();
    }
    if (websiteConfiguration.getRoutingRules() != null && websiteConfiguration.getRoutingRules().size() > 0) {
        final XmlWriter routingRules = xml.start("RoutingRules");
        for (final RoutingRule rule : websiteConfiguration.getRoutingRules()) {
            writeRule(routingRules, rule);
        }
        routingRules.end();
    }
    xml.end();
    return xml.getBytes();
}
Also used : RedirectRule(com.amazonaws.services.s3.model.RedirectRule) RoutingRule(com.amazonaws.services.s3.model.RoutingRule) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

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