AWS Lambda@edge with S3 when calling image

  1. Resize image when uploading: Original image is lost.
  2. Save resized images to AWS S3, along with original images: costs more storages
  3. Resize image when image URL is called using AWS Lambda: the way I will show now
  1. Configure IAM role, since Lambda accesses to S3 via authorization by IAM.
  2. Initialize Lambda function.
  3. Write resizing code.
  4. Apply the code to Lambda function initialized in stage 2.

2.1 .withMetadata()

S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
.resize(width, height)
.withMetadata() // add this line here
.toBuffer()
)

2.2 .rotate()

Sharp(data.Body)
.rotate()
.resize(width, height)
.toBuffer()
‘use strict’;const querystring = require(‘querystring’); // Don’t install.
const AWS = require(‘aws-sdk’); // Don’t install.
const Sharp = require(‘sharp’);
const S3 = new AWS.S3({
// region: ‘Asia Pacific (Seoul) ap-northeast-2’
});
const BUCKET = ‘my bucket name';
exports.handler = async (event, context, callback) => {
const { request, response } = event.Records[0].cf;
// Parameters are w, h, f, q and indicate width, height, format and quality.
const params = querystring.parse(request.querystring);
// Required width or height value.
if (!params.w && !params.h) {
return callback(null, response);
}


// Extract name and format.
const { uri } = request;

const [, imageName, extension] = uri.match(/\/?(.*)\.(.*)/);

// Exception ‘.gif’ image.
if (extension === ‘gif’) {
console.log(‘GIF image requested!’);
return callback(null, response);
}
// Init variables
let width;
let height;
let format;
let quality; // Sharp는 이미지 포맷에 따라서 품질(quality)의 기본값이 다릅니다.
let s3Object;
let resizedImage;
// Init sizes.
width = parseInt(params.w, 10) ? parseInt(params.w, 10) : null;
height = parseInt(params.h, 10) ? parseInt(params.h, 10) : null;
// Init quality.
if (parseInt(params.q, 10)) {
quality = parseInt(params.q, 10);
}
// Init format.
format = params.f ? params.f : extension;
format = format === ‘jpg’ ? ‘jpeg’ : format;
// For AWS CloudWatch.
console.log(`params: ${JSON.stringify(params)}`); // Cannot convert object to primitive value.
console.log(`name: ${imageName}.${extension}`); // Favicon error, if name is `favicon.ico`.
try {
s3Object = await S3.getObject({
Bucket: BUCKET,
Key: decodeURI(imageName + ‘.’ + extension)
}).promise();
} catch (error) {
console.log(‘S3.getObject: ‘, error);
return callback(error);
}
try {
resizedImage = await Sharp(s3Object.Body)
.resize(width, height)
.toFormat(format, {
quality
})
.withMetadata() // add this line here to prevent image rotating
.toBuffer();
} catch (error) {
console.log(‘Sharp: ‘, error);
return callback(error);
}
const resizedImageByteLength = Buffer.byteLength(resizedImage, ‘base64’);
console.log(‘byteLength: ‘, resizedImageByteLength);
// `response.body`가 변경된 경우 1MB까지만 허용됩니다.
if (resizedImageByteLength >= 1 * 1024 * 1024) {
return callback(null, response);
}
response.status = 200;
response.body = resizedImage.toString(‘base64’);
response.bodyEncoding = ‘base64’;
response.headers[‘content-type’] = [
{
key: ‘Content-Type’,
value: `image/${format}`
}
];
return callback(null, response);
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store