Search in sources :

Example 1 with S3Handler

use of com.nextdoor.bender.handler.s3.S3Handler in project bender by Nextdoor.

the class Bender method invokeS3Handler.

protected static void invokeS3Handler(String source_file) throws HandlerException {
    /*
     * https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
     * https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
     */
    String awsRegion = "us-east-1";
    String eventName = "s3:ObjectCreated:Put";
    String eventSource = "aws:s3";
    String eventVersion = "2.0";
    String s3ConfigurationId = "cli-id";
    String s3SchemaVersion = "1.0";
    S3BucketEntity s3BucketEntity = null;
    S3ObjectEntity s3ObjectEntity = null;
    /*
     * Make sure the URL was submitted properly
     *
     * Split the s3://bucket/object path into an S3BucketEntity and S3ObjectEntity object
     */
    try {
        AmazonS3URI s3URI = new AmazonS3URI(source_file);
        s3BucketEntity = new S3BucketEntity(s3URI.getBucket(), null, null);
        s3ObjectEntity = new S3ObjectEntity(s3URI.getKey(), 1L, null, null);
    } catch (IllegalArgumentException e) {
        logger.error("Invalid source_file URL supplied (" + source_file + "): " + e);
        System.exit(1);
    }
    /*
     * Override the AWS Region if its supplied
     */
    if (System.getenv("AWS_REGION") != null) {
        awsRegion = System.getenv("AWS_REGION");
    }
    /*
     * Set the arrival timestamp as the function run time.
     */
    DateTime eventTime = new DateTime().toDateTime();
    /*
     * Generate our context/handler objects.. we'll be populating them shortly.
     */
    TestContext ctx = getContext();
    S3Handler handler = new S3Handler();
    /*
     * Create a S3EventNotification event
     */
    S3Entity s3Entity = new S3Entity(s3ConfigurationId, s3BucketEntity, s3ObjectEntity, s3SchemaVersion);
    S3EventNotificationRecord rec = new S3EventNotificationRecord(awsRegion, eventName, eventSource, eventTime.toString(), eventVersion, null, null, s3Entity, null);
    List<S3EventNotificationRecord> notifications = new ArrayList<S3EventNotificationRecord>(2);
    notifications.add(rec);
    S3EventNotification s3event = new S3EventNotification(notifications);
    /*
     * Invoke handler
     */
    handler.handler(s3event, ctx);
    handler.shutdown();
}
Also used : S3Entity(com.amazonaws.services.s3.event.S3EventNotification.S3Entity) S3Handler(com.nextdoor.bender.handler.s3.S3Handler) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) DateTime(org.joda.time.DateTime) S3BucketEntity(com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity) S3EventNotification(com.amazonaws.services.s3.event.S3EventNotification) S3ObjectEntity(com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity) S3EventNotificationRecord(com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord)

Aggregations

AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 S3EventNotification (com.amazonaws.services.s3.event.S3EventNotification)1 S3BucketEntity (com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity)1 S3Entity (com.amazonaws.services.s3.event.S3EventNotification.S3Entity)1 S3EventNotificationRecord (com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord)1 S3ObjectEntity (com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity)1 TestContext (com.nextdoor.bender.aws.TestContext)1 S3Handler (com.nextdoor.bender.handler.s3.S3Handler)1 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1