Search in sources :

Example 1 with NotificationConfiguration

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

the class EnableNotificationOnABucket method main.

public static void main(String[] args) throws IOException {
    String bucketName = "*** Bucket name ***";
    Regions clientRegion = Regions.DEFAULT_REGION;
    String snsTopicARN = "*** SNS Topic ARN ***";
    String sqsQueueARN = "*** SQS Queue ARN ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        BucketNotificationConfiguration notificationConfiguration = new BucketNotificationConfiguration();
        // Add an SNS topic notification.
        notificationConfiguration.addConfiguration("snsTopicConfig", new TopicConfiguration(snsTopicARN, EnumSet.of(S3Event.ObjectCreated)));
        // Add an SQS queue notification.
        notificationConfiguration.addConfiguration("sqsQueueConfig", new QueueConfiguration(sqsQueueARN, EnumSet.of(S3Event.ObjectCreated)));
        // Create the notification configuration request and set the bucket notification configuration.
        SetBucketNotificationConfigurationRequest request = new SetBucketNotificationConfigurationRequest(bucketName, notificationConfiguration);
        s3Client.setBucketNotificationConfiguration(request);
    } 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 : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions)

Example 2 with NotificationConfiguration

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

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified notification configuration into an XML byte array.
 *
 * @param notificationConfiguration The configuration to convert.
 * @return The XML byte array representation.
 */
public byte[] convertToXmlByteArray(BucketNotificationConfiguration notificationConfiguration) {
    final XmlWriter xml = new XmlWriter();
    xml.start("NotificationConfiguration", "xmlns", Constants.XML_NAMESPACE);
    final Map<String, NotificationConfiguration> configurations = notificationConfiguration.getConfigurations();
    for (final Map.Entry<String, NotificationConfiguration> entry : configurations.entrySet()) {
        final String configName = entry.getKey();
        final NotificationConfiguration config = entry.getValue();
        if (config instanceof TopicConfiguration) {
            xml.start("TopicConfiguration");
            xml.start("Id").value(configName).end();
            xml.start("Topic").value(((TopicConfiguration) config).getTopicARN()).end();
            addEventsAndFilterCriteria(xml, config);
            xml.end();
        } else if (config instanceof QueueConfiguration) {
            xml.start("QueueConfiguration");
            xml.start("Id").value(configName).end();
            xml.start("Queue").value(((QueueConfiguration) config).getQueueARN()).end();
            addEventsAndFilterCriteria(xml, config);
            xml.end();
        } else if (config instanceof CloudFunctionConfiguration) {
            xml.start("CloudFunctionConfiguration");
            xml.start("Id").value(configName).end();
            xml.start("InvocationRole").value(((CloudFunctionConfiguration) config).getInvocationRoleARN()).end();
            xml.start("CloudFunction").value(((CloudFunctionConfiguration) config).getCloudFunctionARN()).end();
            addEventsAndFilterCriteria(xml, config);
            xml.end();
        } else if (config instanceof LambdaConfiguration) {
            xml.start("CloudFunctionConfiguration");
            xml.start("Id").value(configName).end();
            xml.start("CloudFunction").value(((LambdaConfiguration) config).getFunctionARN()).end();
            addEventsAndFilterCriteria(xml, config);
            xml.end();
        }
    }
    xml.end();
    return xml.getBytes();
}
Also used : CloudFunctionConfiguration(com.amazonaws.services.s3.model.CloudFunctionConfiguration) QueueConfiguration(com.amazonaws.services.s3.model.QueueConfiguration) NotificationConfiguration(com.amazonaws.services.s3.model.NotificationConfiguration) BucketNotificationConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration) LambdaConfiguration(com.amazonaws.services.s3.model.LambdaConfiguration) TopicConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration.TopicConfiguration) Map(java.util.Map) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Example 3 with NotificationConfiguration

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

the class BucketConfigurationXmlFactory method addEventsAndFilterCriteria.

private void addEventsAndFilterCriteria(XmlWriter xml, NotificationConfiguration config) {
    for (final String event : config.getEvents()) {
        xml.start("Event").value(event).end();
    }
    final Filter filter = config.getFilter();
    if (filter != null) {
        validateFilter(filter);
        xml.start("Filter");
        if (filter.getS3KeyFilter() != null) {
            validateS3KeyFilter(filter.getS3KeyFilter());
            xml.start("S3Key");
            for (final FilterRule filterRule : filter.getS3KeyFilter().getFilterRules()) {
                xml.start("FilterRule");
                xml.start("Name").value(filterRule.getName()).end();
                xml.start("Value").value(filterRule.getValue()).end();
                xml.end();
            }
            xml.end();
        }
        xml.end();
    }
}
Also used : Filter(com.amazonaws.services.s3.model.Filter) AnalyticsFilter(com.amazonaws.services.s3.model.analytics.AnalyticsFilter) LifecycleFilter(com.amazonaws.services.s3.model.lifecycle.LifecycleFilter) InventoryFilter(com.amazonaws.services.s3.model.inventory.InventoryFilter) S3KeyFilter(com.amazonaws.services.s3.model.S3KeyFilter) MetricsFilter(com.amazonaws.services.s3.model.metrics.MetricsFilter) FilterRule(com.amazonaws.services.s3.model.FilterRule)

Example 4 with NotificationConfiguration

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

the class BucketNotificationConfigurationStaxUnmarshaller method unmarshall.

@Override
public BucketNotificationConfiguration unmarshall(InputStream inputStream) throws Exception {
    final XmlPullParser xpp = xmlPullParserFactory.newPullParser();
    xpp.setInput(inputStream, null);
    final StaxUnmarshallerContext context = new StaxUnmarshallerContext(xpp, null);
    final int originalDepth = context.getCurrentDepth();
    int targetDepth = originalDepth + 1;
    if (context.isStartOfDocument()) {
        targetDepth += 1;
    }
    final BucketNotificationConfiguration config = new BucketNotificationConfiguration();
    while (true) {
        final int xmlEvent = context.nextEvent();
        if (xmlEvent == XmlPullParser.END_DOCUMENT) {
            break;
        } else if (xmlEvent == XmlPullParser.START_TAG) {
            if (context.testExpression("TopicConfiguration", targetDepth)) {
                final Entry<String, NotificationConfiguration> entry = TopicConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
                config.addConfiguration(entry.getKey(), entry.getValue());
            } else if (context.testExpression("QueueConfiguration", targetDepth)) {
                final Entry<String, NotificationConfiguration> entry = QueueConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
                config.addConfiguration(entry.getKey(), entry.getValue());
            } else if (context.testExpression("CloudFunctionConfiguration", targetDepth)) {
                final Entry<String, NotificationConfiguration> entry = LambdaConfigurationStaxUnmarshaller.getInstance().unmarshall(context);
                config.addConfiguration(entry.getKey(), entry.getValue());
            }
        } else if (xmlEvent == XmlPullParser.END_TAG) {
            if (context.getCurrentDepth() < originalDepth) {
                return config;
            }
        }
    }
    return config;
}
Also used : Entry(java.util.Map.Entry) StaxUnmarshallerContext(com.amazonaws.transform.StaxUnmarshallerContext) XmlPullParser(org.xmlpull.v1.XmlPullParser) NotificationConfiguration(com.amazonaws.services.s3.model.NotificationConfiguration) BucketNotificationConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration) BucketNotificationConfiguration(com.amazonaws.services.s3.model.BucketNotificationConfiguration)

Example 5 with NotificationConfiguration

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

the class LambdaConfigurationStaxUnmarshaller method unmarshall.

@Override
public Entry<String, NotificationConfiguration> unmarshall(StaxUnmarshallerContext context) throws Exception {
    final int originalDepth = context.getCurrentDepth();
    int targetDepth = originalDepth + 1;
    if (context.isStartOfDocument()) {
        targetDepth += 1;
    }
    String id = null;
    final List<String> events = new ArrayList<String>();
    Filter filter = null;
    String functionArn = null;
    String invocationRole = null;
    while (true) {
        final int xmlEvent = context.nextEvent();
        if (xmlEvent == XmlPullParser.END_DOCUMENT) {
            break;
        } else if (xmlEvent == XmlPullParser.START_TAG) {
            if (context.testExpression("Id", targetDepth)) {
                id = StringStaxUnmarshaller.getInstance().unmarshall(context);
            } else if (context.testExpression("Event", targetDepth)) {
                events.add(StringStaxUnmarshaller.getInstance().unmarshall(context));
            } else if (context.testExpression("Filter", targetDepth)) {
                filter = FilterStaxUnmarshaller.getInstance().unmarshall(context);
            } else if (context.testExpression("CloudFunction", targetDepth)) {
                functionArn = StringStaxUnmarshaller.getInstance().unmarshall(context);
            } else if (context.testExpression("InvocationRole", targetDepth)) {
                invocationRole = StringStaxUnmarshaller.getInstance().unmarshall(context);
            }
        } else if (xmlEvent == XmlPullParser.END_TAG) {
            if (context.getCurrentDepth() < originalDepth) {
                return createLambdaConfig(id, events, functionArn, invocationRole, filter);
            }
        }
    }
    return null;
}
Also used : Filter(com.amazonaws.services.s3.model.Filter) ArrayList(java.util.ArrayList)

Aggregations

BucketNotificationConfiguration (com.amazonaws.services.s3.model.BucketNotificationConfiguration)2 Filter (com.amazonaws.services.s3.model.Filter)2 NotificationConfiguration (com.amazonaws.services.s3.model.NotificationConfiguration)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 TopicConfiguration (com.amazonaws.services.s3.model.BucketNotificationConfiguration.TopicConfiguration)1 CloudFunctionConfiguration (com.amazonaws.services.s3.model.CloudFunctionConfiguration)1 FilterRule (com.amazonaws.services.s3.model.FilterRule)1 LambdaConfiguration (com.amazonaws.services.s3.model.LambdaConfiguration)1 QueueConfiguration (com.amazonaws.services.s3.model.QueueConfiguration)1 S3KeyFilter (com.amazonaws.services.s3.model.S3KeyFilter)1 AnalyticsFilter (com.amazonaws.services.s3.model.analytics.AnalyticsFilter)1 InventoryFilter (com.amazonaws.services.s3.model.inventory.InventoryFilter)1 LifecycleFilter (com.amazonaws.services.s3.model.lifecycle.LifecycleFilter)1 MetricsFilter (com.amazonaws.services.s3.model.metrics.MetricsFilter)1 StaxUnmarshallerContext (com.amazonaws.transform.StaxUnmarshallerContext)1