Search in sources :

Example 1 with ReplicationRule

use of com.amazonaws.services.s3.model.ReplicationRule 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)

Example 2 with ReplicationRule

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

the class BucketReplicationIntegrationTest method testSetAndRetrieveReplicationConfiguration.

public void testSetAndRetrieveReplicationConfiguration() {
    // Setting new configuration and retrieving.
    BucketReplicationConfiguration configuration = new BucketReplicationConfiguration();
    configuration.addRule(RULE1, new ReplicationRule().withPrefix("testPrefix1").withStatus(ReplicationRuleStatus.Enabled).withDestinationConfig(new ReplicationDestinationConfig().withBucketARN(DEST_BUCKET_ARN)));
    configuration.addRule(RULE2, new ReplicationRule().withPrefix("testPrefix2").withStatus(ReplicationRuleStatus.Enabled).withDestinationConfig(new ReplicationDestinationConfig().withBucketARN(DEST_BUCKET_ARN)));
    euS3.setBucketReplicationConfiguration(SOURCE_BUCKET_NAME, configuration);
    BucketReplicationConfiguration retrievedReplicationConfig = euS3.getBucketReplicationConfiguration(SOURCE_BUCKET_NAME);
    ReplicationRule replRule1 = retrievedReplicationConfig.getRule(RULE1);
    ReplicationRule replRule2 = retrievedReplicationConfig.getRule(RULE2);
    assertNotNull(replRule1);
    assertNotNull(replRule2);
    assertEquals("testPrefix1", replRule1.getPrefix());
    assertEquals(DEST_BUCKET_ARN, replRule1.getDestinationConfig().getBucketARN());
    assertEquals(ROLE, retrievedReplicationConfig.getRoleARN());
    assertEquals("testPrefix2", replRule2.getPrefix());
    assertEquals(DEST_BUCKET_ARN, replRule2.getDestinationConfig().getBucketARN());
    // disabling second rule.
    replRule2.setStatus(ReplicationRuleStatus.Disabled);
    BucketReplicationConfiguration updatedConfiguration = new BucketReplicationConfiguration().addRule(RULE1, replRule1).addRule(RULE2, replRule2).withRoleARN(ROLE);
    euS3.setBucketReplicationConfiguration(SOURCE_BUCKET_NAME, updatedConfiguration);
    GetBucketReplicationConfigurationRequest getBucketReplicationConfigurationRequest = new GetBucketReplicationConfigurationRequest(SOURCE_BUCKET_NAME);
    retrievedReplicationConfig = euS3.getBucketReplicationConfiguration(getBucketReplicationConfigurationRequest);
    replRule1 = retrievedReplicationConfig.getRule(RULE1);
    replRule2 = retrievedReplicationConfig.getRule(RULE2);
    assertNotNull(replRule1);
    assertNotNull(replRule2);
    assertEquals("testPrefix1", replRule1.getPrefix());
    assertEquals(DEST_BUCKET_ARN, replRule1.getDestinationConfig().getBucketARN());
    assertEquals(ReplicationRuleStatus.Enabled.toString(), replRule1.getStatus());
    assertEquals("testPrefix2", replRule2.getPrefix());
    assertEquals(DEST_BUCKET_ARN, replRule2.getDestinationConfig().getBucketARN());
    assertEquals(ReplicationRuleStatus.Disabled.toString(), replRule2.getStatus());
}
Also used : BucketReplicationConfiguration(com.amazonaws.services.s3.model.BucketReplicationConfiguration) ReplicationDestinationConfig(com.amazonaws.services.s3.model.ReplicationDestinationConfig) GetBucketReplicationConfigurationRequest(com.amazonaws.services.s3.model.GetBucketReplicationConfigurationRequest) ReplicationRule(com.amazonaws.services.s3.model.ReplicationRule)

Example 3 with ReplicationRule

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

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

public byte[] convertToXmlByteArray(BucketReplicationConfiguration replicationConfiguration) {
    final XmlWriter xml = new XmlWriter();
    xml.start("ReplicationConfiguration");
    final Map<String, ReplicationRule> rules = replicationConfiguration.getRules();
    final String role = replicationConfiguration.getRoleARN();
    xml.start("Role").value(role).end();
    for (final Map.Entry<String, ReplicationRule> entry : rules.entrySet()) {
        final String ruleId = entry.getKey();
        final ReplicationRule rule = entry.getValue();
        xml.start("Rule");
        xml.start("ID").value(ruleId).end();
        xml.start("Prefix").value(rule.getPrefix()).end();
        xml.start("Status").value(rule.getStatus()).end();
        final ReplicationDestinationConfig config = rule.getDestinationConfig();
        xml.start("Destination");
        xml.start("Bucket").value(config.getBucketARN()).end();
        if (config.getStorageClass() != null) {
            xml.start("StorageClass").value(config.getStorageClass()).end();
        }
        xml.end();
        xml.end();
    }
    xml.end();
    return xml.getBytes();
}
Also used : ReplicationDestinationConfig(com.amazonaws.services.s3.model.ReplicationDestinationConfig) ReplicationRule(com.amazonaws.services.s3.model.ReplicationRule) Map(java.util.Map) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Aggregations

ReplicationDestinationConfig (com.amazonaws.services.s3.model.ReplicationDestinationConfig)3 ReplicationRule (com.amazonaws.services.s3.model.ReplicationRule)3 BucketReplicationConfiguration (com.amazonaws.services.s3.model.BucketReplicationConfiguration)2 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 XmlWriter (com.amazonaws.services.s3.internal.XmlWriter)1 DeleteMarkerReplication (com.amazonaws.services.s3.model.DeleteMarkerReplication)1 GetBucketReplicationConfigurationRequest (com.amazonaws.services.s3.model.GetBucketReplicationConfigurationRequest)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 Map (java.util.Map)1