I have a notification on an S3 bucket upload to place a message in an SQS queue. The SQS queue triggers a lambda function. I am trying to extract the name of the file that was uploaded from the SQS message which triggers the lambda function. My SQS event record looks like this when printed to the CloudWatch logs:
{
"Records": [
{
"eventVersion": "2.1",
"eventSource": "aws:s3",
"awsRegion": "eu-west-2",
"eventTime": "2020-04-05T13:55:30.970Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "A2RFWU4TTDGK95"
},
"requestParameters": {
"sourceIPAddress": "HIDDEN"
},
"responseElements": {
"x-amz-request-id": "024EF2A2E94BD5CA",
"x-amz-id-2": "P/5p5mDwfIu29SeZcNo3wjJaGAiM4yqBqp4p3gOfLVPeZhf+w5sRjnxsost3BuYub1FVf7tuMFs9KoC98+fwSI9NrT5WbjYq"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "ImageUpload",
"bucket": {
"name": "HIDDEN",
"ownerIdentity": {
"principalId": "A2RFWU4TTDGK95"
},
"arn": "arn:aws:s3:::HIDDEN"
},
"object": {
"key": "activity1.png",
"size": 41762,
"eTag": "9e1645a32c2948139a90e75522deb5ab",
"sequencer": "005E89E354A986B50D"
}
}
}
]
}
Using this code:
import boto3
rek = boto3.client('rekognition')
def test(event, context):
for record in event['Records']:
print ("test")
payload=record["body"]
fullpayload=str(payload)
print(fullpayload)
Using ['s3]['object]['key'] to access the filename 'activity1.png' on the payload string gives me this error:
's3': KeyError
Traceback (most recent call last):
How can i access the file name from the lambda function?