1

What is the way to get the response body of the uploaded JSON file to the lambda function to print it? I used the following code but it is specified for Content Type. Any suggestions for this please?

// console.log('Loading function');
        
const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01' });
    
    

exports.handler = async (event, context) => {
        
        //console.log('Received event:', JSON.stringify(event, null, 2));
    
        // Get the object from the event and show its content type
        const bucket = event.Records[0].s3.bucket.name;
        const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
        
        const params = {
            Bucket: bucket,
            Key: key,
        };
        
        try {
            const { ContentType } = await s3.getObject(params).promise();
            console.log('CONTENT TYPE:', ContentType);
            console.log('Body: ', );
            
            console.log("response: " + "I want to print the response body here when a JSON file uploaded")
            
            return ContentType;
        } catch (err) {
            console.log(err);
            const message = `Error getting object ${key} from bucket ${bucket}. Error : ` + err;
            console.log(message);
            throw new Error(message);
        }
    };

1 Answer 1

1

return value of getObject contains Body field.

const { ContentType, Body } = await s3.getObject(params).promise();
console.log('CONTENT TYPE:', ContentType);
console.log('Body: ', Body);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this is working with SDK Version 2 well. But when we use NodeJs V18+ it uses SDK 3 and getting the bucket name and key is a trouble with it.
const bucket = event.Records[0].s3Client.bucket.name; const key = decodeURIComponent(event.Records[0].s3Client.object.key.replace(/\+/g, ' ')); these methods are not support with SDK 3 I guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.