riseres
riseres

Reputation: 3182

How to write file in memory or to file in Lambda aws?

I want to use lambda to get database from dynamodb and generate compute data to cvs file.

Then attach this file with email send to customer.

Step

  1. Get data from dynamodb. (I know how to do it .)

  2. Write to file .CSV ( need help).

Because lambda doesn't have persisted data. How to write to file

  1. Attach .CSV to email and send to customer. ( need help)

Upvotes: 3

Views: 6366

Answers (2)

imjared
imjared

Reputation: 20584

If you're writing a CSV, you'll probably need to stream the data to S3. Lambda has a pretty good example here for streaming image data from a buffer. Obviously you're not using an image but the concept is about the same.

  1. get your data from db
  2. format it how you need it. I'd recommend looking into streams with something like csv-write-stream.
  3. stream data to s3 with something like s3-streaming-upload or the aws-sdk.
  4. as part of the response from the SDK, you'd get the location of your file.
  5. i'd use mandrill (https://mandrillapp.com/api/docs/messages.html) because it's free, easy, and awesome. you can set the attachment content as base64. Yours might look something like this:

    "attachments": [
      {
        "type": "text/csv",
        "name": "myfile.csv",
        "content": new Buffer( myCsvContent ).toString('base64')
       }
    ]
    

I haven't tested this but did something similar recently and this general approach should work for you.

Upvotes: 2

Matthew Kime
Matthew Kime

Reputation: 754

Do you have an existing setup for sending email? You don't necessarily need to save a file to create a file attachment when sending an email.

simply ignore the fs.readFile statement in the response below

Sending mails with attachment via NodeJS

Upvotes: 3

Related Questions