Skip to content

Instantly share code, notes, and snippets.

@hiiamyes
Created December 30, 2020 15:58
Show Gist options
  • Save hiiamyes/0d4d1ca36e542d73a17e88f98fa60c9e to your computer and use it in GitHub Desktop.
Save hiiamyes/0d4d1ca36e542d73a17e88f98fa60c9e to your computer and use it in GitHub Desktop.
image-resizer
"use strict";
const aws = require("aws-sdk");
const jimp = require("jimp");
const s3 = new aws.S3(
process.env.ENV === "production"
? {
apiVersion: "2006-03-01",
}
: {
apiVersion: "2006-03-01",
accessKeyId: "S3RVER",
secretAccessKey: "S3RVER",
s3ForcePathStyle: true,
endpoint: new aws.Endpoint("http://localhost:4569"),
}
);
/**
* @param {s3 event} event
* https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
*/
module.exports.resize = async (event, context) => {
try {
console.log("resize start");
const bucket = event.Records[0].s3.bucket;
const object = event.Records[0].s3.object;
const [_match, _filePath, fileName, fileType] = /(.*\/)?(\w*).(\w*$)/g.exec(
object.key
);
console.log("get object");
const srcImage = await s3
.getObject({
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property
Bucket: bucket.name,
Key: object.key,
})
.promise();
let destImage = await jimp.read(srcImage.Body);
await destImage.resize(320, jimp.AUTO);
destImage = await destImage.getBufferAsync(jimp.MIME_JPEG);
console.log("put object");
await s3
.putObject({
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
Body: destImage,
Bucket: bucket.name,
ContentType: srcImage.ContentType,
Key: ["resizeds/", fileName, ".", fileType].join(""),
})
.promise();
console.log("success");
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Go",
input: event,
},
null,
2
),
};
} catch (error) {
console.log(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment