Skip to content
Advertisement

How Python AWS Lambda interacts specifically with the uploaded file?

I´m trying to do the following: when I upload a file in my s3 storage, the lambda picks this json file and converts it into a csv file.

How can I specify in the lambda code which file must pick?

example of my code in local:

import pandas as pd

df = pd.read_json('movies.json')
df.to_csv('csv-movies.csv')

in this example, I provide the name of the file…but..how can I manage that on a Lambda?

I think I don´t understand how Lambda works…could you give me an example?

Advertisement

Answer

In order to achieve this you will need to use S3 as the event source for your Lambda, there’s a useful tutorial for this provided by AWS themselves and has some sample python code to assist you, you can view it here.

To break it down slightly further and answer how you get the name of the file. The lambda handler will look similar to the following:

def lambda_handler(event, context)

What is important here is the event object. When your event source is the S3 bucket you will be given the name of the bucket and the s3 key in the object which is effectively the path to the file in the S3 bucket. With this information you can do some logic to decide if you want to download the file from that path. If you do, you can use the S3 get_object( ) api call as shown in the tutorial.

Once this file is downloaded it can be used like any other file you would have on your local machine, so you can then proceed to process the json to a CSV. Once it is converted you will presumably want to put it back in S3 and for this you can use the S3 put_object( ) call for this and reuse the information in the event object in order to specify the path.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement