Home > Software engineering >  Print the JSON value from AWS S3 bucket in Lambda function
Print the JSON value from AWS S3 bucket in Lambda function

Time:01-19

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

CodePudding user response:

return value of getObject contains Body field.

const { ContentType, Body } = await s3.getObject(params).promise();
console.log('CONTENT TYPE:', ContentType);
console.log('Body: ', Body);
  • Related