How to extract S3 event info from SQS using Lambda

Viewed 1662

I am pushing S3 event into SQS which is triggering a Lambda function. I am trying to extract the S3 event information, however the SQSEvent object returns back batch of records and each record is of S3Event type.

Logging the message body of each record confirms the S3Event which got forwarded to SQS

private async Task ProcessMessageAsync(SQSEvent.SQSMessage message, ILambdaContext context)
{
    context.Logger.LogLine($"Processed message {message.Body}");
}

and this comes back with

"Records": [ { "eventVersion": "2.1", "eventSource": "aws:s3", "eventName": "ObjectCreated:Put", "s3": { "s3SchemaVersion": "1.0", "bucket": {......} } } ]

How can i cast the message to S3Event object to extract the info out?

thanks

1 Answers

Figured this out,

S3EventNotification e = S3EventNotification.ParseJson(message.Body);
context.Logger.LogLine($"Processed message {e.Records[0].S3.Object.Key}");
Related