Example usage for com.amazonaws.services.s3.model BucketNotificationConfiguration addConfiguration

List of usage examples for com.amazonaws.services.s3.model BucketNotificationConfiguration addConfiguration

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model BucketNotificationConfiguration addConfiguration.

Prototype

public BucketNotificationConfiguration addConfiguration(String name,
        NotificationConfiguration notificationConfiguration) 

Source Link

Document

Adds the given notification configuration to the BucketNotificationConfiguration object

Usage

From source file:io.konig.maven.CreateAwsS3BucketAction.java

License:Apache License

public AwsDeployment from(String path) throws Exception {
    String cfTemplatePresent = System.getProperty("cfTemplatePresent");
    if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) {
        try {//from w  w w . j av  a2 s .co  m
            File file = deployment.file(path);
            ObjectMapper mapper = new ObjectMapper();
            S3Bucket bucket = mapper.readValue(file, S3Bucket.class);
            deployment.verifyAWSCredentials();
            Regions regions = Regions.fromName(bucket.getRegion());
            AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(deployment.getCredential())
                    .withRegion(regions).build();
            String envtName = "";
            if (System.getProperty("environmentName") != null) {
                envtName = System.getProperty("environmentName");
            }
            String bucketName = StringUtils.replaceOnce(bucket.getBucketName(), "${environmentName}", envtName);
            Bucket b = s3client.createBucket(bucketName);

            if (bucket.getNotificationConfiguration() != null) {
                NotificationConfiguration notificationConfiguration = bucket.getNotificationConfiguration();
                if (notificationConfiguration.getTopicConfiguration() != null) {
                    TopicConfiguration topicConfiguration = notificationConfiguration.getTopicConfiguration();
                    BucketNotificationConfiguration s3notificationConfiguration = new BucketNotificationConfiguration();
                    String accountId = "";
                    if (System.getProperty("aws-account-id") != null) {
                        accountId = System.getProperty("aws-account-id");
                    }
                    String topicArn = StringUtils.replaceOnce(topicConfiguration.getTopicArn(),
                            "${aws-account-id}", accountId);
                    com.amazonaws.services.s3.model.TopicConfiguration topicConfig = new com.amazonaws.services.s3.model.TopicConfiguration(
                            topicArn, topicConfiguration.getEventType());
                    s3notificationConfiguration.addConfiguration("snsTopicConfig", topicConfig);

                    s3client.setBucketNotificationConfiguration(bucketName, s3notificationConfiguration);
                }
            }
            if (b != null)
                deployment.setResponse("AWS S3 Bucket is created ::" + b.getName());
        } catch (Exception e) {
            throw e;
        }
    } else {
        deployment.setResponse("S3 Bucket will be created through cloud formation template");
    }
    return deployment;
}