ajhstn
ajhstn

Reputation: 101

Reading data from s3: s3.getObject is not a function

I am trying to read a txt file from S3 to build a response for Alexa. When testing the code in Lambda, i am getting this error. Can anyone see where i am going wrong?

Error

Error handled: s3.getObject is not a function

I have installed the 'aws-sdk' and required the module at the top of my skill's index.js

const s3 = require('aws-sdk/clients/s3')

Handler code. Just to highlight the point, i am using Async/Await and returning a Promise in the goGetS3 function below.

const ChooseStoryIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      handlerInput.requestEnvelope.request.intent.name === 'ChooseStoryIntent';
  },
  async handle(handlerInput) {
    let speechText;
    let options = {
      "Bucket": "stores",
      "Key": "person.txt"
    }
    await goGetS3(options)
      .then((response) => {
        console.log(response),
        console.log(response.Body.toString()),
          speechText = response
      })
      .catch((err) => {
        console.log(err)
        speechText = 'something wrong getting the story'
      })
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .getResponse();
  },
};

goGetS3() function code. I tried two different versions of it, both give me the same error above.

const goGetS3 = function (options) {
    s3.getObject(options, function (err, data) {
      //handle error
      if (err) {
        reject("Error", err);
      }
      //success
      if (data) {
        resolve(data.Body.toString())
      }
    }).promise()
}
// const goGetS3 = function (options) {
//   return new Promise((resolve, reject) => {
//     s3.getObject(options, function (err, data) {
//       //handle error
//       if (err) {
//         reject("Error", err);
//       }
//       //success
//       if (data) {
//         resolve(data.Body.toString())
//       }
//     })
//   })
// }

My code is assembled from the following blogs/articles.

#### EDIT ###

As per @milan-cermak i added this at the top of the page

const AWS = require('aws-sdk/clients/s3')
const s3 = new AWS.S3()

but now get this error

module initialization error: TypeError 
at Object.<anonymous> (/var/task/index.js:6:12) 
at Module._compile (module.js:652:30) 
at Object.Module._extensions..js (module.js:663:10) 
at Module.load (module.js:565:32) 
at tryModuleLoad (module.js:505:12) 
at Function.Module._load (module.js:497:3) 
at Module.require (module.js:596:17) 
at require (internal/module.js:11:18)

Upvotes: 3

Views: 11263

Answers (2)

ajhstn
ajhstn

Reputation: 101

To get this to work, i had to change

const AWS = require('aws-sdk/clients/s3')
const s3 = new AWS.S3()

to

const AWS = require('aws-sdk')
const s3 = new AWS.S3()

I would love to know why i needed to import the entire AWS SDK to get this to work..

Upvotes: 3

Milan Cermak
Milan Cermak

Reputation: 8074

The s3 in your code is not the instance of the S3 client, but just the module. You need to create a new instance of the client first.

const S3 = require('aws-sdk/clients/s3');
const s3 = new S3();

// you can now do s3.getObject

Upvotes: 5

Related Questions