Search code examples
amazon-web-servicesamazon-s3aws-lambda

Cannot parse event in aws lambda


Seems like a noob question but please help me out. I am getting the dict object in event during a lambda call (S3 object create). Now i want to print out the name of the file which is mapped to "key". I cannot parse the event to get this value. I tried event['Records']['object']['key'] but it doesnt work. I tried converting it using json but it gave me indices error. How should i parse it and get the value

`2023-08-16T23:14:08.661+05:00  <class 'dict'>

2023-08-16T23:14:08.661+05:00   {

2023-08-16T23:14:08.661+05:00   "Records": [

2023-08-16T23:14:08.661+05:00   {

2023-08-16T23:14:08.661+05:00   "eventVersion": "2.1",

2023-08-16T23:14:08.661+05:00   "eventSource": "aws:s3",

2023-08-16T23:14:08.661+05:00   "awsRegion": "us-east-1",

2023-08-16T23:14:08.661+05:00   "eventTime": "2023-08-16T18:14:07.433Z",

2023-08-16T23:14:08.661+05:00   "eventName": "ObjectCreated:Put",

2023-08-16T23:14:08.661+05:00   "userIdentity": {

2023-08-16T23:14:08.661+05:00   "principalId": "AWS:AIDAXVFYRQT5SCZ52T3IA"

2023-08-16T23:14:08.661+05:00   },

2023-08-16T23:14:08.661+05:00   "requestParameters": {

2023-08-16T23:14:08.661+05:00   "sourceIPAddress": "58.65.198.81"

2023-08-16T23:14:08.661+05:00   },

2023-08-16T23:14:08.661+05:00   "responseElements": {

2023-08-16T23:14:08.661+05:00   "x-amz-request-id": "K1F5VBSS8ZZFVXZZ",

2023-08-16T23:14:08.661+05:00   "x-amz-id-2": "UQ4qg1t7b0cQKHH3xmhykrqz2Khi0ilIkhV+4rl2zID5U640uw8QHtqGevN/E8FwvW/Mz82C3uILFYeDaJdma8u866k+eIXT"

2023-08-16T23:14:08.661+05:00   },

2023-08-16T23:14:08.661+05:00   "s3": {

2023-08-16T23:14:08.661+05:00   "s3SchemaVersion": "1.0",

2023-08-16T23:14:08.661+05:00   "configurationId": "4c0adbad-3f5a-4bae-b5d4-0d10bce6c058",

2023-08-16T23:14:08.661+05:00   "bucket": {

2023-08-16T23:14:08.661+05:00   "name": "os-lab-demo",

2023-08-16T23:14:08.661+05:00   "ownerIdentity": {

2023-08-16T23:14:08.661+05:00   "principalId": "A1EIYWRSGRMZ3E"

2023-08-16T23:14:08.661+05:00   },

2023-08-16T23:14:08.661+05:00   "arn": "arn:aws:s3:::os-lab-demo"

2023-08-16T23:14:08.661+05:00   },

2023-08-16T23:14:08.661+05:00   "object": {

2023-08-16T23:14:08.661+05:00   "key": "ertugul.jpg",

2023-08-16T23:14:08.661+05:00   "size": 88458,

2023-08-16T23:14:08.661+05:00   "eTag": "92efc540bb3abee495c43e65a3f39697",

2023-08-16T23:14:08.661+05:00   "versionId": "r6hdxaF.0QmuOdPXnIiJt9HP0R3GLnr2",

2023-08-16T23:14:08.661+05:00   "sequencer": "0064DD11EF5F32C4EC"

2023-08-16T23:14:08.661+05:00   }

2023-08-16T23:14:08.661+05:00   }

2023-08-16T23:14:08.661+05:00   }

2023-08-16T23:14:08.661+05:00   ]

2023-08-16T23:14:08.661+05:00   }
`






This is my code in lambda for testing purpose


``
import boto3
import json
client = boto3.client('s3')
def lambda_handler(event, context):
    print (type(event))
    print (json.dumps(event, indent= 4))
    print (event['key'])

``


Solution

  • Records is an array, so you need something like:

    key = event['Records'][0]['s3']['object']['key']