从 s3 读取数据:s3.getObject 不是函数

ajh*_*stn 3 alexa-skills-kit

我正在尝试从 S3 读取 txt 文件来构建 Alexa 的响应。在 Lambda 中测试代码时,我收到此错误。谁能看到我哪里出错了?

错误

Error handled: s3.getObject is not a function
Run Code Online (Sandbox Code Playgroud)

我已经安装了“aws-sdk”,并且需要该模块位于我的技能的 index.js 顶部

const s3 = require('aws-sdk/clients/s3')
Run Code Online (Sandbox Code Playgroud)

处理程序代码。为了强调这一点,我使用 Async/Await 并在下面的 goGetS3 函数中返回一个 Promise。

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();
  },
};
Run Code Online (Sandbox Code Playgroud)

goGetS3()函数代码。我尝试了它的两个不同版本,都给了我上面相同的错误。

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())
//       }
//     })
//   })
// }
Run Code Online (Sandbox Code Playgroud)

我的代码是根据以下博客/文章组装而成的。

#### 编辑 ###

根据@milan-cermak,我将其添加到页面顶部

const AWS = require('aws-sdk/clients/s3')
const s3 = new AWS.S3()
Run Code Online (Sandbox Code Playgroud)

但现在得到这个错误

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)
Run Code Online (Sandbox Code Playgroud)

Mil*_*mak 5

您代码s3中的 不是 S3 客户端的实例,而只是模块。您需要首先创建一个新的客户端实例。

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

// you can now do s3.getObject
Run Code Online (Sandbox Code Playgroud)