【问题标题】:aws lambda s3 function isn't called inside alexa skills kitaws lambda s3 函数未在 alexa 技能包中调用
【发布时间】:2017-07-23 10:20:04
【问题描述】:

我正在尝试为 Amazon Echo 创建一项技能,该技能将从 AWS S3 调用 JSON 文件。当我从 s3 基本 get 函数调用代码时,它可以工作。亚马逊 Alexa 代码可以独立运行。

但是当我将它们一起调用时,函数会被跳过。因此,对于以下代码,控制台在s3.getObject() 之前和之后被调用。但是中间的被跳过了。我不懂为什么。

我还检查了是否调用了 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;
}

【问题讨论】:

    标签: json node.js amazon-web-services amazon-s3 alexa-skills-kit


    【解决方案1】:

    这可能是一个控制流问题,我之前使用过 amazons sdk 并且遇到过类似的问题。尝试在您的代码中实现async,以便更好地控制何时发生。这样方法不会跳过。

    更新:添加一些你可以做什么的代码示例。

    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.
    })
    

    【讨论】:

    • 我尝试使用异步,但我认为我做得不对。这是我尝试过的方法:s3 with async
    • 您正在尝试将同步函数与异步函数混合使用,但这是行不通的,您需要摆脱 return 语句,而是使用回调获取该值。所以最后你基本上会用回调(myData)替换return myData。我现在不在,但我稍后会更新我的答案。希望这能让您了解如何解决您的问题。
    • @ManjurKhan 我已经用代码示例更新了我的答案。
    • 感谢您的帮助,但我仍然无法让它工作,所以我目前正在使用 python 从 s3 获取信息。
    【解决方案2】:

    这是一个时间问题。下面是一个在会话启动时从 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());
            }
        });        
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 1970-01-01
      • 2018-10-07
      • 2018-01-28
      • 1970-01-01
      • 2017-08-03
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多