简体   繁体   English

alexa技能套件中未调用AWS Lambda s3函数

[英]aws lambda s3 function isn't called inside alexa skills kit

I am trying to create a skill for Amazon Echo that will call a JSON file from AWS S3. 我正在尝试为Amazon Echo创建一项技能,该技能将从AWS S3调用JSON文件。 When I call the code from s3 basic get function it works. 当我从s3基本get函数调用代码时,它可以工作。 And the Amazon Alexa code works on its own. 而且Amazon Alexa代码可以独立工作。

But when I call them together the function gets skipped. 但是当我一起调用它们时,该功能将被跳过。 So for the following code the console gets called before and after s3.getObject() . 因此,对于以下代码,控制台在s3.getObject()之前和之后s3.getObject() But the middle one gets skipped. 但是中间的一个被跳过了。 I do not understand why. 我不理解为什么。

I also checked whether s3 was being called, and it is. 我还检查了是否调用了s3,确实如此。

let aws = require('aws-sdk');
let s3 = new aws.S3({ apiVersion: '2006-03-01'});

function callS3() {
    console.log('loading S3 function');
    var myData = [];

    const params = {
        Bucket: 'cvo-echo',
        Key: 'data.json'
    };
    console.log("trying to get s3");
    s3.getObject(params, (err, data) => {
        if (err) {
            console.log('error in s3 get: \n' + err);
            //const message = `Error getting object ${key} from bucket ${bucket}.
            // Make sure they exist and your bucket is in same region as this function.
            //console.log(message);
        } else {
            console.log('CONTENT TYPE: ', data.ContentType);
            console.log('Data body: \n' + data.Body.toString());
            myData = JSON.parse(data.Body.toString());
            console.log('myData.length = ' + myData.length);
        }
        console.log('myData >> ' + myData);
    });

    console.log('finished callS3() func');
    return myData;
}

This might be a control flow issue, I've worked with amazons sdk before and was running into similar issues. 这可能是一个控制流程问题,我之前曾与Amazon sdk合作,并且遇到了类似的问题。 Try implementing async within your code to have a better control of what happens when. 尝试在代码中实现异步 ,以便更好地控制何时发生。 This way methods won't skip. 这样,方法就不会跳过。

UPDATE: adding some code examples of what you could do. 更新:添加一些示例代码,您可以做什么。

function callS3(callback) {
    console.log('loading S3 function');
    var myData = [];

    const params = {
        Bucket: 'cvo-echo',
        Key: 'data.json'
    };
    console.log("trying to get s3");
    s3.getObject(params, (err, data) => {
        if (err) {
            console.log('error in s3 get: \n' + err);
            //const message = `Error getting object ${key} from bucket ${bucket}.
            // Make sure they exist and your bucket is in same region as this function.
            //console.log(message);

            callback(err,null);//callback the error.
        } else {
            console.log('CONTENT TYPE: ', data.ContentType);
            console.log('Data body: \n' + data.Body.toString());
            myData = JSON.parse(data.Body.toString());
            console.log('myData.length = ' + myData.length);

            console.log('myData >> ' + myData);
            console.log('finished callS3() func');

            //Include the callback inside of the S3 call to make sure this function returns until the S3 call completes.
            callback(null,myData); // first element is an error and second is your data, first element is null if no error ocurred.
        }
    });

}

/*
This MIGHT work without async but just in case you can read more about 
async.waterfall where functions pass down values to the next function.
*/

async.waterfall([
    callS3()//you can include more functions here, the callback from the last function will be available for the next.
    //myNextFunction()
],function(err,myData){
    //you can use myData here.
})

It's a timing issue. 这是一个时间问题。 Here is an example of loading a JSON file from an S3 share when a session is started. 这是启动会话时从S3共享加载JSON文件的示例。

function onLaunch(launchRequest, session, callback) {
    var sBucket = "your-bucket-name";
    var sFile = "data.json";
    var params = {Bucket: sBucket, Key: sFile};
    var s3 = new AWS.S3();
    var s3file = s3.getObject(params)

    new AWS.S3().getObject(params, function(err, data) {
        if (!err) {
            var json = JSON.parse(new Buffer(data.Body).toString("utf8"));
            for(var i = 0; i < json.length; i++) {
                 console.log("name:" + json[i].name + ", age:" + json[i].age);
            }                
            getWelcomeResponse(callback);                
        } else {
            console.log(err.toString());
        }
    });        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AWS Lambda函数中使用S3中的JSON文件(用于Alexa) - Using a JSON file from S3 in AWS Lambda function (for Alexa) 在JSON对象中写出Boto3响应,并在AWS Lambda函数中上传到S3 - Write out Boto3 Response in JSON Object and Upload to S3 in AWS Lambda Function Merging multiple JSON files into single JSON file in S3 from AWS Lambda python function - Merging multiple JSON files into single JSON file in S3 from AWS Lambda python function 无法在没有任何 HTTP 错误的情况下从 AWS Lambda 中的 S3 下载文件 - Can't download file from S3 in AWS Lambda without any HTTP error 如何使用 AWS Lambda 函数从 S3 解码 a.gz 文件? - How can I decode a .gz file from S3 using an AWS Lambda function? 从python(lambda)中的s3事件数据中提取AWS区域 - Extracting aws region from s3 event data in python (lambda) 使用lambda函数解析Alex错误的Alexa - Alexa with lambda function parsing json error 如何在 Lambda 函数中读取 S3 文件(在 python 中) - How to read S3 file in Lambda function(in python) 在AWS Glus pyspark作业中从s3加载JSON - Load JSON from s3 inside aws glue pyspark job 如果 object 不使用 aws s3 加密或 aws:kms 加密,则 Json 拒绝 object 上传到 aws s3 的策略脚本 - Json policy script for denying object upload to aws s3 if the object doesn't uses aws s3 encryption or aws:kms encryption
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM