@Override public void init(final BlobStoreConfiguration configuration) { this.blobStoreConfiguration = configuration; try { this.s3 = amazonS3Factory.create(configuration); if (!s3.doesBucketExist(getConfiguredBucket())) { s3.createBucket(getConfiguredBucket()); addBucketLifecycleConfiguration(null); } else { // bucket exists, we should test that the correct lifecycle config is present BucketLifecycleConfiguration lifecycleConfiguration = s3.getBucketLifecycleConfiguration(getConfiguredBucket()); if (!isExpirationLifecycleConfigurationPresent(lifecycleConfiguration)) { addBucketLifecycleConfiguration(lifecycleConfiguration); } } setConfiguredBucket(getConfiguredBucket()); } catch (Exception e) { throw new BlobStoreException("Unable to initialize blob store bucket: " + getConfiguredBucket(), e, null); } }
boolean isExpirationLifecycleConfigurationPresent(BucketLifecycleConfiguration lifecycleConfiguration) { return lifecycleConfiguration != null && lifecycleConfiguration.getRules() != null && lifecycleConfiguration.getRules().stream() .filter(r -> r.getExpirationInDays() == getConfiguredExpirationInDays()) .filter(r -> { LifecycleFilterPredicate predicate = r.getFilter().getPredicate(); if (predicate instanceof LifecycleTagPredicate) { LifecycleTagPredicate tagPredicate = (LifecycleTagPredicate) predicate; return DELETED_TAG.equals(tagPredicate.getTag()); } return false; }) .findAny().isPresent(); }
BucketLifecycleConfiguration makeLifecycleConfiguration(BucketLifecycleConfiguration existing, int expirationInDays) { BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule() .withId(LIFECYCLE_EXPIRATION_RULE_ID) .withFilter(new LifecycleFilter( new LifecycleTagPredicate(DELETED_TAG))) .withExpirationInDays(expirationInDays) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); if (existing != null) { existing.getRules().add(rule); return existing; } else { return new BucketLifecycleConfiguration().withRules(rule); } }
public static void main(String[] args) throws Exception { System.out.println( "Enter the number of days to keep objects before delete:" ); String days = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); System.out.println( "Enter the prefix (i.e. folder1/) to identify specific objects to delete:" ); String prefix = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); // create the AWS S3 Client AmazonS3 s3 = AWSS3Factory.getS3Client(); // create the new rule BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule() .withExpirationInDays(Integer.valueOf(days)) .withId("rule-1") .withPrefix(prefix.trim()) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); // build the rule into configuration type BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration(Arrays.asList(rule)); // save the lifecycle policy s3.setBucketLifecycleConfiguration(AWSS3Factory.S3_BUCKET, configuration); // Retrieve configuration. BucketLifecycleConfiguration result = s3.getBucketLifecycleConfiguration(AWSS3Factory.S3_BUCKET); System.out.println(String.format("bucket lifecycle configuration: prefix is %s and days is %s.", result.getRules().get(0).getPrefix(), result.getRules().get(0).getExpirationInDays())); //s3.deleteBucketLifecycleConfiguration(AWSS3Factory.S3_BUCKET); }
boolean isExpirationLifecycleConfigurationPresent(BucketLifecycleConfiguration lifecycleConfiguration) { return lifecycleConfiguration != null && lifecycleConfiguration.getRules() != null && lifecycleConfiguration.getRules().stream() .filter(r -> r.getExpirationInDays() == getConfiguredExpirationInDays()) .anyMatch(r -> { LifecycleFilterPredicate predicate = r.getFilter().getPredicate(); if (predicate instanceof LifecycleTagPredicate) { LifecycleTagPredicate tagPredicate = (LifecycleTagPredicate) predicate; return DELETED_TAG.equals(tagPredicate.getTag()); } return false; }); }
BucketLifecycleConfiguration makeLifecycleConfiguration(BucketLifecycleConfiguration existing, int expirationInDays) { BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule() .withId(LIFECYCLE_EXPIRATION_RULE_ID) .withFilter(new LifecycleFilter( new LifecycleTagPredicate(DELETED_TAG))) .withExpirationInDays(expirationInDays) .withStatus(BucketLifecycleConfiguration.ENABLED); if (existing != null) { existing.getRules().add(rule); return existing; } else { return new BucketLifecycleConfiguration().withRules(rule); } }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) { return delegate.getBucketLifecycleConfiguration(bucketName); }
@Override public void setBucketLifecycleConfiguration(String bucketName, BucketLifecycleConfiguration bucketLifecycleConfiguration) { delegate.setBucketLifecycleConfiguration(bucketName, bucketLifecycleConfiguration); }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest) { return delegate.getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest); }
private void addBucketLifecycleConfiguration(BucketLifecycleConfiguration lifecycleConfiguration) { s3.setBucketLifecycleConfiguration( getConfiguredBucket(), makeLifecycleConfiguration(lifecycleConfiguration, getConfiguredExpirationInDays())); }
/** * 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 SdkClientException { XmlWriter xml = new XmlWriter(); xml.start("LifecycleConfiguration"); for (Rule rule : config.getRules()) { writeRule(xml, rule); } xml.end(); return xml.getBytes(); }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) { return call(() -> getDelegate().getBucketLifecycleConfiguration(bucketName)); }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest) { return call(() -> getDelegate().getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest)); }
@Override public void setBucketLifecycleConfiguration(String bucketName, BucketLifecycleConfiguration bucketLifecycleConfiguration) { run(() -> getDelegate().setBucketLifecycleConfiguration(bucketName, bucketLifecycleConfiguration)); }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) { return null; }
@Override public void setBucketLifecycleConfiguration(String bucketName, BucketLifecycleConfiguration bucketLifecycleConfiguration) { }
public void run() { AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration()); if (endpoint.contains("amazonaws.com")) { String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket)); if (aws_endpoint.contains("US")) { s3Client.setEndpoint("https://s3.amazonaws.com"); } else if (aws_endpoint.contains("us-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("eu-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("ap-")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("sa-east-1")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else { s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com"); } } else { s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build()); s3Client.setEndpoint(endpoint); } int converted_days = 0; if (!disabled) { converted_days = Integer.parseInt(days); } Transition transToArchive = new Transition() .withDays(converted_days) .withStorageClass(StorageClass.Glacier); BucketLifecycleConfiguration.Rule ruleArchiveAndExpire = null; if (!disabled) { ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule() .withPrefix(prefix) .withTransition(transToArchive) // .withExpirationInDays(converted_days + 1) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); } else { ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule() .withPrefix(prefix) .withTransition(transToArchive) //.withExpirationInDays(100) .withStatus(BucketLifecycleConfiguration.DISABLED.toString()); } List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>(); rules.add(ruleArchiveAndExpire); try { BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration() .withRules(rules); s3Client.setBucketLifecycleConfiguration(bucket, configuration); } catch (Exception get) { mainFrame.jTextArea1.append("\n" + get.getMessage()); } if (!disabled) { mainFrame.jTextArea1.append("\nSent request to set bucket life cycle to tier to Glacier after: " + converted_days + " day(s). Please observe for any errors."); } else { mainFrame.jTextArea1.append("\nSent request to disable the bucket life cycle. Please observe for any errors."); } calibrate(); }
public void run() { AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration()); if (endpoint.contains("amazonaws.com")) { String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket)); if (aws_endpoint.contains("US")) { s3Client.setEndpoint("https://s3.amazonaws.com"); } else if (aws_endpoint.contains("us-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("eu-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("ap-")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("sa-east-1")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else { s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com"); } } else { s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build()); s3Client.setEndpoint(endpoint); } int converted_days = 0; if (!disabled) { converted_days = Integer.parseInt(days); } BucketLifecycleConfiguration.Rule ruleArchiveAndExpire = null; if (!disabled) { ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule() .withPrefix(prefix) .withExpirationInDays(converted_days) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); } else { ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule() .withPrefix(prefix) .withExpirationInDays(100) .withStatus(BucketLifecycleConfiguration.DISABLED.toString()); } List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>(); rules.add(ruleArchiveAndExpire); try { BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration() .withRules(rules); s3Client.setBucketLifecycleConfiguration(bucket, configuration); } catch (Exception get) { mainFrame.jTextArea1.append("\n" + get.getMessage()); } if (!disabled) { mainFrame.jTextArea1.append("\nSent request to change bucket life cycle to " + converted_days + " day(s). Please observe for any errors."); } else { mainFrame.jTextArea1.append("\nSent request to disable the bucket life cycle. Please observe for any errors."); } calibrate(); mainFrame.jPanel9.setVisible(true); }
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) { // TODO Auto-generated method stub return null; }
@Override public void setBucketLifecycleConfiguration(String bucketName, BucketLifecycleConfiguration bucketLifecycleConfiguration) { // TODO Auto-generated method stub }