Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ const {
} = require("@aws-sdk/client-s3");
const s3 = new S3Client();

const createTargetKey = (str) => {
return str.replace(/u\//, "new/");
};

exports.handler = async (event) => {
console.log(JSON.stringify(event, null, 2));
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
const targetKey = createTargetKey(key);

const params = {
Bucket: bucket,
Expand All @@ -27,15 +21,15 @@ exports.handler = async (event) => {

if (!stream) throw new Error("BodyStream is empty");

const resizedImage = await sharp(Buffer.concat(await stream.toArray()))
const resizedImage = await sharp(await stream)
.resize({ width: 220, height: 220, fit: "cover" })
.webp({ quality: 80 })
.toBuffer();

await s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: targetKey,
Key: key,
Body: resizedImage,
}),
);
Expand Down
75 changes: 75 additions & 0 deletions cdk/lambdas/uploadResize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const sharp = require("sharp");
const {
S3Client,
GetObjectCommand,
PutObjectCommand,
} = require("@aws-sdk/client-s3");
const s3 = new S3Client();

// Define the sizes
const sizes = [
{ maxWidth: 640, maxHeight: 640, suffix: "640w" },
{ maxWidth: 720, maxHeight: 720, suffix: "720w" },
{ maxWidth: 750, maxHeight: 750, suffix: "750w" },
{ maxWidth: 786, maxHeight: 786, suffix: "786w" },
{ maxWidth: 828, maxHeight: 828, suffix: "828w" },
{ maxWidth: 1100, maxHeight: 1100, suffix: "1100w" },
{ maxWidth: 1400, maxHeight: 1400, suffix: null }, // Original image override
];

exports.handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;

const params = {
Bucket: bucket,
Key: key,
};

try {
const response = await s3.send(new GetObjectCommand(params));
const stream = response.Body;

if (!stream) throw new Error("BodyStream is empty");

const imageRaw = await stream;

// Function to resize an image
const resizeImage = async (buffer, size) => {
return sharp(buffer)
.resize({
width: size.maxWidth,
height: size.maxHeight,
fit: "inside",
withoutEnlargement: false,
}) // Fits within maxWidth and maxHeight
.webp({ quality: 80 })
.toBuffer();
};

// Loop through sizes and upload each resized image
for (const size of sizes) {
const resizedImage = await resizeImage(imageRaw, size);

const newKey = size.suffix
? key.replace(/(\.[\w\d_-]+)$/i, `_${size.suffix}$1`)
: key;

await s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: newKey,
Body: resizedImage,
}),
);
}

return {
statusCode: 200,
body: JSON.stringify({ message: "Images resized successfully!" }),
};
} catch (error) {
console.error("Error processing image:", error);
throw new Error("Error processing image");
}
};
Loading