skip to Main Content

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


  1. it is b/c you are using an async function as a callback.
    here

    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;
        });
    

    remove it. it is not used

    Login or Signup to reply.
  2. 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:

    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));
            return new Promise((resolve, reject) => {
                stream.on('end', () => {
                    const data = Buffer.concat(items).toString('utf8').split(',');
                    console.log(data);
                    const sw = data.includes('ChannelType') ? true : false;
                    if (!sw) {
                        reject(new Error("ChannelType not found"));
                    } else {
                        resolve(sw);
                    }
                });
            });
        } catch (error) {
            console.log(error);
            throw error;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search