title: Uploading and reading an image from s3 via postman and lambda
body: <p>I am trying to write a code where I am sending an image or array of images via multipart/form-data as in the below images:</p>
  
  <p><a href="https://i.stack.imgur.com/VPwmU.png" rel="nofollow noreferrer">Click here for the postman Screenshot</a></p>
  
  <p>I am trying to store these images to an S3 bucket and then trigger another lambda to read the images to perform text extraction using AWS rekognition.</p>
  
  <p>Code for the lambda that is storing the images is as below:</p>
  
  <pre><code>var AWS = require('aws-sdk')
  var express = require('express')
  var multer = require('multer')
  var multerS3 = require('multer-s3')
  var bodyParser = require('body-parser')
  const cors = require('cors')
  
  var app = express()
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cors())
  
  const env = process.env.NODE_ENV || 'prod'
  
  AWS.config.update({
      accessKeyId: 'ACCESS_KEY',
      secretAccessKey: 'SECRET_KEY',
      region: 'ap-south-1'
  });
  
  var s3 = new AWS.S3({
      region: "ap-south-1"
  })
  
  var lambda = new AWS.Lambda({
      region: "ap-south-1"
  });
  
  var upload = multer({
      storage: multerS3({
          s3: s3,
          bucket: 'MY_BUCKET_NAME',
          metadata: function (req, file, cb) {
              cb(null, { fieldName: file.fieldname });
          },
          key: function (req, file, cb) {
              req.originalname = file.originalname
              cb(null, req.originalname)
          }
      })
  })
  
  app.post('/', upload.array('file', 2), function (req, res, next) {
  
      const params = {
          FunctionName: "ANOTHER_LAMBDA_NAME",
          Payload: JSON.stringify({ "fileName": req.originalname })
      };
  
      return lambda.invoke(params, (err, data) =&gt; {
          if (err) console.log("err", err.stack);
          else {
              res.json(data.Payload)
          }
      });
  })
  
  if (env === "dev") {
      const port = process.env.port || 4000
      app.listen(port, () =&gt; console.log(`server is running on ${port}`))
  }
  else {
      module.exports = app
  }
  </code></pre>
  
  <p>Code for another lambda is as below:</p>
  
  <pre><code>var AWS = require('aws-sdk')
  var s3 = new AWS.S3()
  
  const rekognition = new AWS.Rekognition()
  
  exports.handler = function(event, context) {
    var params = {
      Image: { 
        S3Object: {
          Bucket: 'MY_BUCKET_NAME',
          Name: event.filename,
        }
      }
    };
  
    rekognition.detectText(params, function(err, data) {
      if (err) console.log(err, err.stack); 
      else     context.succeed(data);           // returns "null"
    });
  };
  </code></pre>
  
  <p>After this, I created an API Gateway which triggers the first lambda to upload the files in s3 as in the picture above. The response I get is <strong>"null"</strong></p>
  
  <p>The main problem seems to be that the image is getting stored in S3 Bucket. But when I download that image and try to open it, it gives the following error:
  <code>The file “EPICared.jpg” could not be opened. It may be damaged or use a file format that Preview doesn’t recognize.</code></p>
  
  <p>So what do you guys suggest I should be doing? Where am I going wrong? I cannot figure out why the image cannot be opened or read by the other lambda.</p>