Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created June 10, 2022 13:44
Show Gist options
  • Save alanshaw/dbecbf2b5e05114702914d51bb2e04e9 to your computer and use it in GitHub Desktop.
Save alanshaw/dbecbf2b5e05114702914d51bb2e04e9 to your computer and use it in GitHub Desktop.
SNSEvent to S3Event
/**
* Extract an S3Event from the passed SNSEvent.
* @param {import('aws-lambda').SNSEvent} snsEvent
* @returns {import('aws-lambda').S3Event}
*/
function toS3Event (snsEvent) {
const s3Event = { Records: [] }
for (const snsRec of snsEvent.Records) {
try {
for (const s3Rec of JSON.parse(snsRec.Sns.Message).Records || []) {
if (s3Rec.eventSource !== 'aws:s3') continue
s3Event.Records.push(s3Rec)
}
} catch (err) {
console.error(`failed to extract S3Event record from SNSEvent record: ${err.message}`)
}
}
return s3Event
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment