0

I want to put the entry in dynamo-db table,as soon as i put object in S3 file. I have set the triggers for the same.

I have written simple below lambada code to check the response

def lambda_handler(event, context):
    print(str(event))

When i am checking in CLoudWatchLogs i can see below.

{'Records': [{'eventVersion': '2.1', 'eventSource': 'aws:s3', 
'awsRegion': 'us-east-1', 'eventTime': '2019-02-20T15:58:48.057Z', 
'eventName': 'ObjectCreated:Put', 'userIdentity': {'principalId': 
'AHRO8PUVZ6HA1'}, 'requestParameters': {'sourceIPAddress': 
'106.193.192.255'}, 'responseElements': {'x-amz-request-id': 
'218359C00133F5A3', 'x-amz-id-2': 
 'ZcA26gyzVOaaG+O1AgbZl6UTGjdqxvz0THfeyGepwPwkn8No2m+
 2jCm/zHvMGH1igrATG1pAnQI='}, 's3': {'s3SchemaVersion': '1.0', 
'configurationId': 'd4eeba8b-3cab-43d2-835a-2d6fed343144', 'bucket': 
 {'name': 'trus-bucket-291902', 'ownerIdentity': {'principalId': 
 'AHRO8PUVZ6HA1'}, 'arn': 'arn:aws:s3:::trus-bucket-291902'}, 'object': 
 {'key': 'Linux-admin-Lecture-14.pdf', 'size': 113847, 'eTag': 
'a8b3f353134bfaf7e876e05b439c7632', 'sequencer': '005C6D7937E630A6EB'}}}]}

Can you help me fetch the object name(Linux-admin-Lecture-14.pdf) and size (113847) from above string?

1
  • Please format your code sample to look more readable Commented Feb 20, 2019 at 16:34

1 Answer 1

1

The event is a normal Python dictionary, so it's accessed the usual way

key = event['Records'][0]['s3']['object']['key']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Pasi.I appreciate if you can explain me how you arrive to this so that next time i can do by my own.
You can look up tutorials explaining python dictionaries, for example: w3schools.com/python/python_dictionaries.asp You can also go to a Json viewer website that can nicely format it, so that you can see which keys to use to access it (for ex jsonviewer.stack.hu paste your code and hit "format")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.