1

I want to return a resized image through a aws lambda function using sharp but I can't seem to find out how to return the resized image from the promise as a response



const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01' });
global.Buffer = global.Buffer || require('buffer').Buffer
const sharp = require('sharp');

exports.handler = async (event, context) => {

    
    let image = event["queryStringParameters"]["image"];
    
    const params = {
        Bucket: "my bucket",
        Key: image,
    };
    const {Body} = await s3.getObject(params).promise();


    sharp(Body)
    .resize(402, 400)
    .toFormat('jpeg')
    .toBuffer()
    .then(data => {
        response = {
            "statusCode": 200,
            "headers": {
                "Content-Type": "image/jpeg"
            },
            "body": data.toString('base64'),
            "isBase64Encoded": true
        };
    })
    .catch(err => {
        response=err;
    })




    
    return response;
};

I already tried returning the promise itself and using resolve() and it doesn't seem to change anything.

zlp980
  • 11
  • 2
  • Looks like `toBuffer()` returns a promise, so await that before creating and returning a response? – jarmod Feb 10 '23 at 20:23

0 Answers0