Untitled

 avatar
unknown
plain_text
5 months ago
4.8 kB
8
Indexable
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class GenericEventHandler implements RequestHandler<Object, String> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String handleRequest(Object input, Context context) {
        try {
            // Convert the input to a JSON string for inspection
            String jsonEvent = objectMapper.writeValueAsString(input);

            // Parse the event as a JsonNode
            JsonNode rootNode = objectMapper.readTree(jsonEvent);

            // Check if this is an S3 event
            if (isS3Event(rootNode)) {
                // Manually extract the fields and create S3EventNotificationRecord
                S3Event s3Event = createS3Event(rootNode);

                // Handle the S3 event
                handleS3Event(s3Event, context);
            } else {
                // Handle other event types or log unknown event
                System.out.println("Unknown event type: " + jsonEvent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Event processed";
    }

    // Method to check if the event is an S3 event by inspecting the "Records" structure
    private boolean isS3Event(JsonNode rootNode) {
        // Check if the event contains "Records" with an "eventSource" of "aws:s3"
        if (rootNode.has("Records") && rootNode.get("Records").isArray()) {
            for (JsonNode record : rootNode.get("Records")) {
                if (record.has("eventSource") && "aws:s3".equals(record.get("eventSource").asText())) {
                    return true;
                }
            }
        }
        return false;
    }

    // Method to manually create S3Event from JsonNode
    private S3Event createS3Event(JsonNode rootNode) {
        List<S3EventNotification.S3EventNotificationRecord> recordsList = new ArrayList<>();

        JsonNode recordsNode = rootNode.get("Records");
        if (recordsNode != null && recordsNode.isArray()) {
            for (JsonNode recordNode : recordsNode) {
                // Extract bucket and object information
                JsonNode s3Node = recordNode.get("s3");
                if (s3Node != null) {
                    JsonNode bucketNode = s3Node.get("bucket");
                    JsonNode objectNode = s3Node.get("object");

                    // Create bucket and object entities
                    S3EventNotification.S3BucketEntity bucket = new S3EventNotification.S3BucketEntity(
                            bucketNode.get("name").asText(),
                            null,
                            bucketNode.get("arn").asText());

                    S3EventNotification.S3ObjectEntity object = new S3EventNotification.S3ObjectEntity(
                            objectNode.get("key").asText(),
                            objectNode.get("size").asLong(),
                            objectNode.get("eTag").asText(),
                            null);

                    // Create S3 entity
                    S3EventNotification.S3Entity s3Entity = new S3EventNotification.S3Entity("1.0", bucket, object, null);

                    // Create S3EventNotificationRecord
                    S3EventNotification.S3EventNotificationRecord record = new S3EventNotification.S3EventNotificationRecord(
                            recordNode.get("awsRegion").asText(),
                            recordNode.get("eventName").asText(),
                            recordNode.get("eventSource").asText(),
                            recordNode.get("eventTime").asText(),
                            s3Entity,
                            null,
                            null,
                            null);

                    recordsList.add(record);
                }
            }
        }

        // Return the constructed S3Event
        return new S3Event(recordsList);
    }

    // Handle S3 event using the S3Event class
    private void handleS3Event(S3Event s3event, Context context) {
        for (S3EventNotification.S3EventNotificationRecord record : s3event.getRecords()) {
            String bucketName = record.getS3().getBucket().getName();
            String objectKey = record.getS3().getObject().getKey();
            System.out.println("Bucket: " + bucketName);
            System.out.println("Object Key: " + objectKey);
        }
    }
Editor is loading...
Leave a Comment