Created
July 4, 2015 11:48
-
-
Save ikait/be0ebef7e931e85da477 to your computer and use it in GitHub Desktop.
AWS Lambda で WebP変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// 全体の設定 | |
var DST_BUCKET = 'xxxxxxxxxxx'; // WebP が格納されるバケット | |
var SRC_BUCKET = 'xxxxxxxxxxx'; // イベントソース | |
var ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXX'; // アクセスキーID | |
var SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // シークレットキー | |
var REGION = 'ap-northeast-1'; // リージョン | |
var ACCEPTED_EXTENTIONS = ["png", "jpg", "gif"]; // 拡張子 | |
// WebP に関する設定 | |
var WEBP_QUALITY = 30; | |
var WEBP_PRESET = 'text'; | |
// for node-webp | |
process.env.PATH = process.env.LAMBDA_TASK_ROOT; | |
process.env.LD_LIBRARY_PATH = process.env.LAMBDA_TASK_ROOT; | |
var AWS = require('aws-sdk'); | |
var CWebp = require('cwebp').CWebp; | |
var path = require('path'); | |
AWS.config.region = REGION; | |
AWS.config.update({ accessKeyId: ACCESS_KEY, secretAccessKey: SECRET_KEY }); | |
var s3 = new AWS.S3(); | |
exports.handler = function (event, context) { | |
var params = { | |
Bucket: event.Records[0].s3.bucket.name, | |
Key: event.Records[0].s3.object.key | |
}; | |
if (params.Bucket !== SRC_BUCKET) { | |
context.done(null, 'Invalid bucket: ' + params.Bucket); | |
} | |
var accepted_extentions = ACCEPTED_EXTENTIONS.map(function (v) { | |
return '.' + v + '$'; | |
}); | |
if (!params.Key.match(new RegExp(accepted_extentions.join('|')))) { | |
context.done(null, 'It seems no image file: ' + params.Key); | |
} | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
console.log('Received context:', JSON.stringify(context, null, 2)); | |
s3.getObject(params, function (error, data) { | |
if (error) { | |
context.fail('Error getting ' + params.Key + ' from ' + params.Bucket); | |
context.done(); | |
} | |
var webp = new CWebp(data.Body); | |
webp.quality(WEBP_QUALITY); | |
webp.preset(WEBP_PRESET); | |
webp.toBuffer().then(function (buffer) { | |
s3.putObject({ | |
Bucket: DST_BUCKET, | |
Key: params.Key.replace(path.extname(params.Key), '.webp'), | |
Body: buffer | |
}, function (error, data) { | |
if (error) { | |
console.log('error: ' + error + ' stack: ' + error.stack); | |
context.fail('error: ' + error + ' stack: ' + error.stack); | |
} else { | |
console.log('data: ' + data); | |
context.succeed('data: ' + data); | |
} | |
}); | |
})['catch'](function (e) { | |
context.fail('Error converting ' + params.Key + ' to WebP from ' + params.Bucket + '. ' + e); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting error with this. Any idea on this?
{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}