Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: read csv file dimensions from an s3 bucket with Python without using Pandas or CSV package

good afternoon. I am hoping that someone can help me with this issue.

I have multiple CSV files that are sitting in an s3 folder. I would like to use python without the Pandas, and the csv package (because aws lambda has very limited packages available, and there is a size restriction) and loop through the files sitting in the s3 bucket, and read the csv dimensions (length of rows, and length of columns)

For example my s3 folder contains two csv files (1.csv, and 2 .csv) my code will run through the specified s3 folder, and put the count of rows, and columns in 1 csv, and 2 csv, and puts the result in a new csv file. I greatly appreciate your help! I can do this using the Pandas package (thank god for Pandas, but aws lambda has restrictions that limits me on what I can use)

AWS lambda uses python 3.7

like image 772
xboxuser Avatar asked Jul 05 '26 21:07

xboxuser


1 Answers

If you can visit your s3 resources in your lambda function, then basically do this to check the rows,

def lambda_handler(event, context):
    import boto3 as bt3
    s3 = bt3.client('s3')
    csv1_data = s3.get_object(Bucket='the_s3_bucket', Key='1.csv')
    csv2_data = s3.get_object(Bucket='the_s3_bucket', Key='2.csv')

    contents_1 = csv1_data['Body'].read()
    contents_2 = csv2_data['Body'].read()
    rows1 = contents_1.split()
    rows2=contents_2.split()    
    return len(rows1), len(rows2)

It should work directly, if not, please let me know. BTW, hard coding the bucket and file name into the function like what I did in the sample is not a good idea at all.

Regards.

like image 94
tim Avatar answered Jul 07 '26 14:07

tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!