dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 795 forks source link

Upload image issue #464

Closed dfloresgonz closed 4 months ago

dfloresgonz commented 6 years ago

My image seems to be corrupted when uploaded to server, It only works with .txt files.

I'm using sls 1.27, EspressJS and multiparty Is there a way to upload images without any problems?

Thanks.

function uploadImage(req, res) {
    let multiparty = require('multiparty');
    let fs = require('fs');
    let form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
        console.log(files);
        //uploading file...
        })).then(result => {
            res.status(200).send({ msj : 'Images were uploaded'});
        }).catch(err => {
            res.status(500).send(err);
        });
    });
}
const serverless = require('serverless-http');
const express = require('express')
const app = express()
require('./middlewares/authenticated');

var bodyParser = require('body-parser');
var cors       = require('cors');

app.use(cors({'origin' : '*'}));
app.use(bodyParser.urlencoded({ extended : false }) )
   .use(bodyParser.json());
service: my-backend

provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: us-east-1

plugins:
  - serverless-offline
  - serverless-s3-local

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
  upload:
    handler: index.handler
    events:
      - http: 'POST /api/registro/uploadImage/'
kennyhyun commented 8 months ago

basically, this package is mainly for testing serverless (for example lambda) locally. so this package would be enough to support usual way for serverless functions.

I believe it is discouraged to send any binary data to serverless functions. if that payload was not that big, base64 encoding should be an alternative.

I was doing this in a dodgy way to use this package in some special case, not only for testing. if someone need this, I think it should take their own risk. so I won't send a PR.

I agree with this BTW

For whomever is still messing around with this, small advice, is not worth it. You will shortly realize that lambdas have 6MB upload limits, so you better start to look into alternatives. This dude put together a good article: https://theburningmonk.com/2020/04/hit-the-6mb-lambda-payload-limit-heres-what-you-can-do/

corentindesfarges commented 4 months ago

BTW, I was patching node_modules/serverless-offline/dist/events/http/HttpServer.js

for my own purpose

-      request.payload = request.payload && request.payload.toString(encoding);
+      if (request.payload && encoding !== 'binary') {
+        request.payload = request.payload.toString(encoding);
+      }

Thank you, works perfectly in v13.3.3!

Edit : This PR appears to fix the issue. :-)

DorianMazur commented 4 months ago

Fixed by #1776