【问题标题】:s3.getObject inside lambda doesn't return anythinglambda 内的 s3.getObject 不返回任何内容
【发布时间】:2017-01-18 16:18:06
【问题描述】:

我在 lambda 函数内有以下代码,这是 Amazon 回显技能:

"AMAZON.HelpIntent": function (intent, session, response) {
  var speechOutput ="Start";

  // Try S3
  var s3 = new AWS.S3({httpOptions: { timeout: 2000 }});
  var params = {
    Bucket: 'bucket',
    Key: 'file',
  };

  s3.getObject(params, function (err, data) {
    if (err) {
      // console.log(err, err.stack);
      speechOutput += "inside error";
      speechOutput += "Did not get it!" + err + ":===:" + err.stack;
    }
    else {
      speechOutput += "inside success";
      // console.log(data);
      speechOutput += "Got it! :" + data.Body.toString('ascii');
    }
  });

  speechOutput += " End. ";

  var repromptText = "Help reprompt.";
  response.ask(speechOutput, repromptText);
},

看起来很简单......但是,当技能被执行时,响应是这样的:

    {
          "version": "1.0",
          "response": {
            "outputSpeech": {
              "type": "PlainText",
              "text": "Start End. "
            },
            "shouldEndSession": false,
            "reprompt": {
              "outputSpeech": {
                "type": "PlainText",
                "text": "Help reprompt."
              }
            }
          },
          "sessionAttributes": {}
   }

换句话说,s3.getObject 不会抛出任何错误,也不会运行。

lamda 函数具有“lambda_basic_execution”角色,在 IAM 中定义为对 S3 具有完全访问权限:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": "*"
    }
  ]
}

在 S3 内部,有一个存储桶(名为“bucket”)和文件(名为“file”)。文件可从 CLI 和 Web 访问。存储桶和文件的权限设置为“所有人”。

请帮忙。

【问题讨论】:

    标签: node.js amazon-web-services amazon-s3 lambda


    【解决方案1】:

    您无需等待 S3 调用完成。代码中的最后 3 行在调用 s3.getObject() 回调之前执行。尝试通过将最后 3 行移到回调中来更改代码,如下所示:

    "AMAZON.HelpIntent": function (intent, session, response) {
      var speechOutput ="Start";
    
      // Try S3
      var s3 = new AWS.S3({httpOptions: { timeout: 2000 }});
      var params = {
        Bucket: 'bucket',
        Key: 'file',
      };
    
      s3.getObject(params, function (err, data) {
        if (err) {
          // console.log(err, err.stack);
          speechOutput += "inside error";
          speechOutput += "Did not get it!" + err + ":===:" + err.stack;
        }
        else {
          speechOutput += "inside success";
          // console.log(data);
          speechOutput += "Got it! :" + data.Body.toString('ascii');
        }
    
    
        speechOutput += " End. ";
    
        var repromptText = "Help reprompt.";
        response.ask(speechOutput, repromptText);
      });
    },
    

    【讨论】:

    • 太棒了!这绝对改变了事情!现在出现超时错误,但进度不减!!!谢谢!
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 2020-01-10
    • 2019-08-09
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多