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;
}
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);
}
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());
}
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();
}
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();
}
Aggregations