0

我正在尝试为 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;
}
4

2 回答 2

0

这是一个时间问题。下面是一个在会话启动时从 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());
        }
    });        
}
于 2017-07-23T02:11:01.417 回答
0

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

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

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.
})
于 2016-07-15T23:25:49.047 回答