数据中心每五分钟将一个csv文件上传到我们的S3存储桶,这触发了我的lambda函数读取文件并将数据保存到DynamoDB。但是执行数据持久性的代码不稳定,有时会被执行,有时会被完全跳过。这让我很困惑。这是我的代码。
var AWS = require('aws-sdk');
var csvtojson = require('csvtojson');
var encoding = require('text-encoding');
AWS.config.update({
accessKeyId: '********',
secretAccessKey: '**********',
region: 'us-west-2',
sslEnabled:false
});
var s3 = new AWS.S3();
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
await res.map(async(item,key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
let putRes = await ddb.putItem(putParams).promise();
console.dir(putRes);
});
}
catch(error){
console.error(error);
return error;
}
};
答案 0 :(得分:1)
Array.map()
返回的数组不是Promise,因此您不能await
(例如代码中的await res.map()
)。
首先,您应该收集一个诺言列表,并使用Promise.all()
等待所有诺言。
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
// Construct the list of promises.
const promises = res.map((item, key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
return ddb.putItem(putParams).promise();
});
// Wait for all promises to finish.
return Promise.all(promises)
}
catch(error){
console.error(error);
return error;
}
};