forked from ysugimoto/aws-lambda-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (35 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Automatic Image resize, reduce with AWS Lambda
* Lambda main handler
*
* @author Yoshiaki Sugimoto
* @created 2015/10/29
*/
"use strict";
const ImageProcessor = require("./libs/ImageProcessor");
const Config = require("./libs/Config");
const fs = require("fs");
const path = require("path");
// Lambda Handler
exports.handler = (event, context) => {
const s3Object = event.Records[0].s3;
const configPath = path.resolve(__dirname, "config.json");
const processor = new ImageProcessor(s3Object);
const config = new Config(
JSON.parse(fs.readFileSync(configPath, { encoding: "utf8" }))
);
processor.run(config)
.then((processedImages) => {
var message = "OK, " + processedImages.length + " images were processed.";
console.log(message);
context.succeed(message);
})
.catch((messages) => {
if ( messages === "Object was already processed." ) {
console.log("Image already processed");
context.succeed("Image already processed");
} else {
context.fail("Error processing " + s3Object.object.key + ": " + messages);
}
});
};