title: aws upload object to S3 bucket and pass details of data to lambda
body: <p>Working my way through tutorials for AWS...So ive created an S3 bucket which when a file is dropped into it calls my lambda 'testHelloWorld' which sends an email...this all works fine (see below)</p>
  
  <pre><code> 'use strict';
  
  console.log('Loading function');
  
  var aws = require('aws-sdk');
  var ses = new aws.SES({
     region: 'us-west-2'
  });
  
  exports.handler = function(event, context) {
      console.log("Incoming: ", event);
     // var output = querystring.parse(event);
  
      var eParams = {
          Destination: {
              ToAddresses: ["johnb@hotmail.com"]
          },
          Message: {
              Body: {
                  Text: {
                      Data: "Hey! What is up?"
                  }
              },
              Subject: {
                  Data: "Email Subject!!!"
              }
          },
          Source: "johnb@hotmail.com"
      };
  
      console.log('===SENDING EMAIL===');
      var email = ses.sendEmail(eParams, function(err, data){
          if(err) console.log(err);
          else {
              console.log("===EMAIL SENT===");
              console.log(data);
  
  
              console.log("EMAIL CODE END");
              console.log('EMAIL: ', email);
              context.succeed(event);
  
          }
      });
  
  };
  </code></pre>
  
  <p>but I want to extend the email to include data on the file that was uploaded to the bucket. I have found <a href="https://stackoverflow.com/questions/43314871/how-to-trigger-my-lambda-function-once-the-file-is-uploaded-to-s3-bucket">How to trigger my Lambda Function once the file is uploaded to s3 bucket</a> which gives a node.js code snippet which should capture the data. I have tried to import this into my existing lambda</p>
  
  <pre><code> 'use strict';
  
  console.log('Loading function');
  
  var aws = require('aws-sdk');
  var ses = new aws.SES({
     region: 'us-west-2'
  });
  var s3 = new aws.S3({ apiVersion: '2006-03-01', accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY, region: process.env.LAMBDA_REGION });
  
  exports.handler = function(event, context, exit){
      console.log("Incoming: ", event);
     // var output = querystring.parse(event);
  
  
   // Get the object from the event and show its content type
     // const bucket = event.Records[0].s3.bucket.name;
     // const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
      const params = {
         Bucket: 'bucketName',
         Key: 'keyName',
         Source : 'SourceName',
         Destination : 'DestinationName',
         Message : 'MessageName'
      };
  
  
       s3.getObject(function(err, data){
           if (err) {
             console.log('ERROR ' + err);
            // exit(err);
           } else {
             // the data has the content of the uploaded file
             var eParams = {
          Destination: {
              ToAddresses: ["johnboy@hotmail.com"]
          },
          Message: {
              Body: {
                  Text: {
                      Data: data
                  }
              },
              Subject: {
                  Data: "Email Subject!!!"
              }
          },
          Source: "johnboy@hotmail.com"
      };
           }
       });     
  
  
      console.log('===SENDING EMAIL===');
      var email = ses.sendEmail(eParams, function(err, data){
          if(err) console.log(err);
          else {
              console.log("===EMAIL SENT===");
              console.log(data);
  
  
              console.log("EMAIL CODE END");
              console.log('EMAIL: ', email);
              context.succeed(event);
  
          }
      });
  
  };
  </code></pre>
  
  <p>but this is failing on the params</p>
  
  <pre><code>message: 'There were 3 validation errors:
  </code></pre>
  
  <p>* MissingRequiredParameter: Missing required key \'Source\' in params
  * MissingRequiredParameter: Missing required key \'Destination\' in params
  * MissingRequiredParameter: Missing required key \'Message\' in params',
    code: 'MultipleValidationErrors',
    errors: </p>
  
  <p>These source, destination and message are listed in the params, are they not correctly formatted and it isnt picking them up?</p>
  
  <p>I cant find much online....any help appreciated</p>
  
  <p><strong>UPDATE</strong>
  Ok iv got it working without failing...if i use the test function in the lambda with the following code...</p>
  
  <pre><code>   'use strict';
  
  console.log('Loading function');
  
  var aws = require('aws-sdk');
  var ses = new aws.SES({
     region: 'us-west-2'
  });
  
  var s3 = new aws.S3({ apiVersion: '2006-03-01', accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY, region: process.env.LAMBDA_REGION });
  
  
  exports.handler = function(event, context) {
      console.log("Incoming: ", event);
     // var output = querystring.parse(event);
  
     var testData = null;
  
     // Get the object from the event and show its content type
     // const bucket = event.Records[0].s3.bucket.name;
     // const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
      const params = {
         Bucket: 'bucket',
         Key: 'key',
      };
  
       s3.getObject(params, function(err, data){
           if (err) {
             console.log('ERROR ' + err);
             exit(err);
           } else {
             testData = data;
           }
       }); 
  
      var eParams = {
          Destination: {
              ToAddresses: ["jim@him.com"]
          },
          Message: {
              Body: {
                  Text: { Data: 'testData2' + testData}
              },
              Subject: {
                  Data: "Email Subject!!!"
              }
          },
          Source: "jim@him.com"
      };
  
      console.log('===SENDING EMAIL===');
      var email = ses.sendEmail(eParams, function(err, data){
          if(err) console.log(err);
          else {
              console.log("===EMAIL SENT===");
              console.log(data);
  
  
              console.log("EMAIL CODE END");
              console.log('EMAIL: ', email);
              context.succeed(event);
  
          }
      });
  
  };
  </code></pre>
  
  <p>I get the email with the body- testData2null</p>
  
  <p>So I tried uploading an image through the s3 bucket and I still get the email with the body testData2null</p>
  
  <p>is there anyway to debug this further or does anyone kno who it is saying null. I never actually tested the code from the other post which passes the data over to the email I just assumed it would work. <code>Does anyone else know who to obtain the data from the upload please? thanks</code></p>