aws lambda s3功能不会在alexa技能套件内部调用
问题描述:
我正在尝试为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;
}
答
这可能是一个控制流问题,我之前和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共享加载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());
}
});
}
我试过使用异步,但我不认为我做得很好。这里是我如何尝试它:[s3与异步](http://pastebin.com/aGZLq4Xt) –
你正试图混合同步功能与异步功能,这是行不通的,你需要摆脱你的回报语句,而是使用回调来获取该值。所以最后你会基本上用回调(myData)替换返回myData。我目前正在出门,但稍后我会更新我的答案。希望这给你一些关于如何解决你的问题的见解。 –
@ManjurKhan我用代码示例更新了我的答案。 –