Example usage for com.amazonaws.services.s3 AmazonS3 setBucketNotificationConfiguration

List of usage examples for com.amazonaws.services.s3 AmazonS3 setBucketNotificationConfiguration

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 setBucketNotificationConfiguration.

Prototype

public void setBucketNotificationConfiguration(String bucketName,
        BucketNotificationConfiguration bucketNotificationConfiguration)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Sets the notification configuration for the specified bucket.

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 {//  www .j  a  v  a2  s .  c om
            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;
}