0

AWS S3 から JSON ファイルを呼び出す Amazon Echo のスキルを作成しようとしています。s3 basic get 関数からコードを呼び出すと、機能します。また、Amazon 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

これは制御フローの問題である可能性があります。以前に 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.
})
于 2016-07-15T23:25:49.047 に答える
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 に答える