Search code examples
javaspring-bootaws-sdkamazon-sqsspring-cloud-aws

Java class for AWS S3 to SQS Event in the SDK


I wonder if there are any ready-to-use java classes in the aws-sdk or spring-cloud-aws for standard SQS events like S3 "ObjectCreated:Put" or if I need to create them on my own.

I didn't find any and all examples seem to use very simple event bodies but maybe I'm blind.

Example for what I'm talking about:

{
  "Records": [
    {
      "eventVersion": "2.1",
      "eventSource": "aws:s3",
      "awsRegion": "eu-central-1",
      "eventTime": "2024-04-19T08:43:56.425Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "AWS:AKJSDGHJKAHSDKJAASD"
      },
      "requestParameters": {
        "sourceIPAddress": "92.79.75.114"
      },
      "responseElements": {
        "x-amz-request-id": "JIOFAHJNEWJDFNASD",
        "x-amz-id-2": "lvTSuzcW9K77xJUIuSTn3d7B6Hz3dqw4KNd8Fc3tsMiA8g9P+B+9JoBKkUZC0aSB1Xm/7US7i/fHSfBPfuiZSgEwUumlYpaL"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "demo-s3-to-sqs",
        "bucket": {
          "name": "upload-demo-123123123",
          "ownerIdentity": {
            "principalId": "ASJKDHNASDJKLASD"
          },
          "arn": "arn:aws:s3:::upload-demo-42342312"
        },
        "object": {
          "key": "123123/mydocument.pdf",
          "size": 11977091,
          "eTag": "cf8580c55a4babf566976531bfd467d8",
          "sequencer": "0066222ECC27CC73BA"
        }
      }
    }
  ]
}

Solution

  • You can parse your JSON to a S3EventNotification class using this:

    S3EventNotification eventNotification = S3EventNotification.parseJson(yourJsonString);
    

    Having this you can get the S3EventNotificationRecord from the list of records.

    Based on: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/event/S3EventNotification.html#parseJson-java.lang.String-