I need your help. The lambda is executed when I upload a csv file to S3,
the file must have a column named ChannelType If it does not have it then it should throw an error and end the execution by displaying "ChannelType not found" in the console.
The problem is that it does, but it does not finish the execution. What is my mistake?
I share my code.
const { GetObjectCommand, S3Client } = require('@aws-sdk/client-s3');
const region = process.env.REGION;
const clientS3 = new S3Client({ region: region });
exports.handler = async (event, context) => {
try {
const key = event.Records[0].s3.object.key;
const bucket = event.Records[0].s3.bucket.name;
await existsChannel(bucket, key);
// continue if there is no error.
} catch (error) {
console.log(error);
}
};
const existsChannel = async (bucket, key) => {
try {
const params = {
Bucket: bucket,
Key: key,
}
const command = new GetObjectCommand( params );
const response = await clientS3.send(command);
const stream = response.Body;
const items = [];
stream.on('data', (item) => items.push(item));
stream.on('end', async () => {
const data = Buffer.concat(items).toString('utf8').split(',');
console.log(data);
const sw = data.includes('ChannelType') ? true : false;
if (!sw) {
throw new Error("ChannelType not found");
}
return sw;
});
} catch (error) {
console.log(error);
}
}
2
Answers
it is b/c you are using an async function as a callback.
here
remove it. it is not used
You don’t need to use the async keyword for the ‘end’ event you can remove it or if you want to do this asynchronously you should refactor the existsChannel function. Here is the refactored code: