title: How do I access an S3 object from a Lambda function?
body: <p>I am attempting to access a text file stored in an AWS S3 bucket. At present, it contains only the word &quot;Test&quot;.</p>
  <p>At first, I thought I was having problems with fs.readfile, but now I've discovered that the problem is more fundamental. I cannot access the file at all. AWS from Node.js does not appear to be able to see the file at all.</p>
  <p>I am using the following Lambda function:</p>
  <pre><code>const aws = require('aws-sdk');
  const s3 = new aws.S3({ apiVersion: '2006-03-01' });
  
      exports.handler = async (event, context, callback) =&gt; { 
      // Get the object from the event and show its content type
      const bucket = event.Records[0].s3.bucket.name;
      const key = event.Records[0].s3.object.key; // decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
      console.log('Bucket = ' + bucket);
      console.log('key = ' + key);
  
      var params = {Bucket: bucket, Key: key};
      console.log('Checking file existence');
      console.log(params);
      console.log('Calling s3.getObject');
      s3.getObject(params, function(err, data) { 
          console.log('S3.getObject called');
          console.log('err = ' + err);
          if (err) {
              console.log(err, err.stack); // an error occurred
              callback(err);
          } else {
              console.log(data);           // successful response
              callback(null, null);
          }
          console.log('Leaving s3.getObject');
      });
  };
  </code></pre>
  <p>The testbed function contains the following code:</p>
  <pre><code>{
    &quot;Records&quot;: [
      {
        &quot;eventVersion&quot;: &quot;2.0&quot;,
        &quot;eventSource&quot;: &quot;aws:s3&quot;,
        &quot;awsRegion&quot;: &quot;us-east-1&quot;,
        &quot;eventTime&quot;: &quot;1970-01-01T00:00:00.000Z&quot;,
        &quot;eventName&quot;: &quot;ObjectCreated:Put&quot;,
        &quot;userIdentity&quot;: {
          &quot;principalId&quot;: &quot;EXAMPLE&quot;
        },
        &quot;requestParameters&quot;: {
          &quot;sourceIPAddress&quot;: &quot;127.0.0.1&quot;
        },
        &quot;responseElements&quot;: {
          &quot;x-amz-request-id&quot;: &quot;EXAMPLE123456789&quot;,
          &quot;x-amz-id-2&quot;: &quot;EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH&quot;
        },
        &quot;s3&quot;: {
          &quot;s3SchemaVersion&quot;: &quot;1.0&quot;,
          &quot;configurationId&quot;: &quot;testConfigRule&quot;,
          &quot;bucket&quot;: {
            &quot;name&quot;: &quot;wgtiplists&quot;,
            &quot;ownerIdentity&quot;: {
              &quot;principalId&quot;: &quot;EXAMPLE&quot;
            },
            &quot;arn&quot;: &quot;arn:aws:s3:::wgtiplists&quot;
          },
          &quot;object&quot;: {
            &quot;key&quot;: &quot;tiplist.txt&quot;,
            &quot;size&quot;: 1024,
            &quot;eTag&quot;: &quot;0123456789abcdef0123456789abcdef&quot;,
            &quot;sequencer&quot;: &quot;0A1B2C3D4E5F678901&quot;
          }
        }
      }
    ]
  }
  </code></pre>
  <p>And the results look like this (I've trimmed off the timestamp and Request ID texts):</p>
  <pre><code>INFO    Bucket = s3://wgtiplists
  INFO    key = tiplist.txt
  INFO    Checking file existence
  INFO    { Bucket: 's3://wgtiplists', Key: 'tiplist.txt' }
  INFO    Calling s3.getObject
  </code></pre>
  <p>From this, I conclude that the S3 function is not being called, although I might be mistaken.</p>
  <p>What am I doing wrong?</p>