jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.43k stars 126 forks source link

Uploading files #157

Open rama41222 opened 4 years ago

rama41222 commented 4 years ago

How to I handle files / multipart form data using lambda-api ?

sebastianDejoy commented 4 years ago

yes please, busboy is limited...multer rules on NodeJS. Please add support to that.

sebastianDejoy commented 4 years ago

Any insight about piping on this framework?

alejuanito commented 3 years ago

Hi, were you able to process a multipart / form-data file? is it possible to do it with this library?

sebastianDejoy commented 3 years ago

Hey @alejuanito, yeap i was. The best approach is to use presigned urls that have a reasonable payload limit (50GB) as opposed to lambda/APIGW that is around 6MB.

I enabled an endpoint that return a presigned url:

const AWS = require('aws-sdk')

const s3 = new AWS.S3()
AWS.config.update({accessKeyId: 'id-omitted', secretAccessKey: 'key-omitted'})

// Tried with and without this. Since s3 is not region-specific, I don't
// think it should be necessary.
// AWS.config.update({region: 'us-west-2'})

const myBucket = 'bucket-name'
const myKey = 'test.pdf'
const signedUrlExpireSeconds = 3600;

const url = s3.getSignedUrl('getObject', {
    Bucket: myBucket,
    Key: myKey,
    Expires: signedUrlExpireSeconds
})

return url; //return this url as a value in your lambda response.

Then your API consumers (mobile aps) can upload whatever file they want through that presigned url: curl -v --upload-file ${fileName} ${presigned_url}

Make sure to grant access from your S3 bucket.