Search in sources :

Example 1 with ReplicationPrefixPredicate

use of com.amazonaws.services.s3.model.replication.ReplicationPrefixPredicate in project aws-doc-sdk-examples by awsdocs.

the class CrossRegionReplication method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String accountId = "*** Account ID ***";
    String roleName = "*** Role name ***";
    String sourceBucketName = "*** Source bucket name ***";
    String destBucketName = "*** Destination bucket name ***";
    String prefix = "Tax/";
    String roleARN = String.format("arn:aws:iam::%s:role/%s", accountId, roleName);
    String destinationBucketARN = "arn:aws:s3:::" + destBucketName;
    AmazonS3 s3Client = AmazonS3Client.builder().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
    createBucket(s3Client, clientRegion, sourceBucketName);
    createBucket(s3Client, clientRegion, destBucketName);
    assignRole(roleName, clientRegion, sourceBucketName, destBucketName);
    try {
        // Create the replication rule.
        List<ReplicationFilterPredicate> andOperands = new ArrayList<ReplicationFilterPredicate>();
        andOperands.add(new ReplicationPrefixPredicate(prefix));
        Map<String, ReplicationRule> replicationRules = new HashMap<String, ReplicationRule>();
        replicationRules.put("ReplicationRule1", new ReplicationRule().withPriority(0).withStatus(ReplicationRuleStatus.Enabled).withDeleteMarkerReplication(new DeleteMarkerReplication().withStatus(DeleteMarkerReplicationStatus.DISABLED)).withFilter(new ReplicationFilter().withPredicate(new ReplicationPrefixPredicate(prefix))).withDestinationConfig(new ReplicationDestinationConfig().withBucketARN(destinationBucketARN).withStorageClass(StorageClass.Standard)));
        // Save the replication rule to the source bucket.
        s3Client.setBucketReplicationConfiguration(sourceBucketName, new BucketReplicationConfiguration().withRoleARN(roleARN).withRules(replicationRules));
        // Retrieve the replication configuration and verify that the configuration
        // matches the rule we just set.
        BucketReplicationConfiguration replicationConfig = s3Client.getBucketReplicationConfiguration(sourceBucketName);
        ReplicationRule rule = replicationConfig.getRule("ReplicationRule1");
        System.out.println("Retrieved destination bucket ARN: " + rule.getDestinationConfig().getBucketARN());
        System.out.println("Retrieved priority: " + rule.getPriority());
        System.out.println("Retrieved source-bucket replication rule status: " + rule.getStatus());
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : ReplicationPrefixPredicate(com.amazonaws.services.s3.model.replication.ReplicationPrefixPredicate) AmazonS3(com.amazonaws.services.s3.AmazonS3) ReplicationDestinationConfig(com.amazonaws.services.s3.model.ReplicationDestinationConfig) BucketReplicationConfiguration(com.amazonaws.services.s3.model.BucketReplicationConfiguration) HashMap(java.util.HashMap) ReplicationFilterPredicate(com.amazonaws.services.s3.model.replication.ReplicationFilterPredicate) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) ReplicationRule(com.amazonaws.services.s3.model.ReplicationRule) SdkClientException(com.amazonaws.SdkClientException) ReplicationFilter(com.amazonaws.services.s3.model.replication.ReplicationFilter) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) DeleteMarkerReplication(com.amazonaws.services.s3.model.DeleteMarkerReplication)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 BucketReplicationConfiguration (com.amazonaws.services.s3.model.BucketReplicationConfiguration)1 DeleteMarkerReplication (com.amazonaws.services.s3.model.DeleteMarkerReplication)1 ReplicationDestinationConfig (com.amazonaws.services.s3.model.ReplicationDestinationConfig)1 ReplicationRule (com.amazonaws.services.s3.model.ReplicationRule)1 ReplicationFilter (com.amazonaws.services.s3.model.replication.ReplicationFilter)1 ReplicationFilterPredicate (com.amazonaws.services.s3.model.replication.ReplicationFilterPredicate)1 ReplicationPrefixPredicate (com.amazonaws.services.s3.model.replication.ReplicationPrefixPredicate)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1