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