[英]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.