0

我正在嘗試爲Amazon Echo創建一項技能,它將從AWS S3中調用JSON文件。當我從s3基本get函數調用代碼時,它可以工作。亞馬遜Alexa的代碼可以獨立運作。aws lambda s3功能不會在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; 
} 

回答

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

我試過使用異步,但我不認爲我做得很好。這裏是我如何嘗試它:[s3與異步](http://pastebin.com/aGZLq4Xt) –

+0

你正試圖混合同步功能與異步功能,這是行不通的,你需要擺脫你的回報語句,而是使用回調來獲取該值。所以最後你會基本上用回調(myData)替換返回myData。我目前正在出門,但稍後我會更新我的答案。希望這給你一些關於如何解決你的問題的見解。 –

+0

@ManjurKhan我用代碼示例更新了我的答案。 –

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()); 
     } 
    });   
} 
相關問題