0

I want to return a csv file that I created in aws lambda function

import json
def lambda_handler(event, context):
    myFile = df.to_csv('sample.csv)
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

This is the default response, how can I return myFile in response

6
  • Why are you not opting to write this to an S3 bucket location and returned a presigned url for the object? Commented Apr 7, 2022 at 11:12
  • 1
    stackoverflow.com/questions/45193012/… Commented Apr 7, 2022 at 11:13
  • @OluwafemiSule Of course I can do that it's my last resort, but my file is in kb and I want to return the file as an attachment in the api response if possible Commented Apr 7, 2022 at 11:32
  • 2
    What you're asking is not currently possible with AWS lambda. You need to always return a response value that is serializable as JSON String in your event handler. Commented Apr 7, 2022 at 11:38
  • 1
    would you like to download this file directly or is your requesting system also one developed by your own? if yes, just send your data as base64 encoded string and just decode it on your client side and save it as CSV... but keep in mind, that the KB from today, could be MB tomorrow, and than you will reach the limit of the responses of API Gateway/Lambda...so if there is the chance, that the csv will grow in future, try to implement the way using S3 (or do manually add gzip your csv before base64 encoding g) Commented Apr 8, 2022 at 20:19

0

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.