Last active
March 3, 2023 13:47
-
-
Save dudash/8db05454253d265a3d89c3ecc9597871 to your computer and use it in GitHub Desktop.
Simple AWS Lambda function to process an image using imageMagik and dump to S3 (triggered by S3 PUT)
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 AWS = require('aws-sdk'); | |
//var im = require('imagemagick'); | |
var im = require("gm").subClass({ imageMagick: true }); | |
var fs = require('fs'); | |
var async = require('async'); | |
// DEBUGGING | |
// console.log('ENV VARS:'); | |
// console.log('-----------------------'); | |
// console.log(JSON.stringify(process.env)); | |
// console.log('-----------------------'); | |
//AWS Configurations (not sure why I have to set these) | |
AWS.config.update({region: 'us-east-1'}); | |
AWS.config.update({sessionToken:process.env.AWS_SESSION_TOKEN, accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY}); | |
// get reference to S3 client | |
var s3 = new AWS.S3(); | |
// response object returned from the lambda function | |
var output = []; | |
var inbucket = process.env.S3INBUCKET; | |
var outbucket = process.env.S3OUTBUCKET; | |
//----------------------------------------------------------------------------- | |
const process_image = (image_key) => { | |
console.log('started processing a image - ' + image_key); | |
var get_params = {Bucket:inbucket, Key:image_key}; | |
s3.getObject(get_params, function(err, response) { | |
if (err) { | |
console.log('Failed to download the file - ' + image_key); | |
console.log(err, err.stack); | |
} | |
else { | |
console.log('Successfully downloaded the file - ' + image_key); | |
const dest_key = 'mo-' + image_key | |
console.log('START image operation for - ' + dest_key); | |
var filename = dest_key.replace(/^.*[\\\/]/, ''); | |
var newFile = "/tmp/" + filename; | |
try { | |
// the operation to process the image and save to /tmp/[newFile] | |
var image = im(response.Body).negative().write(newFile, function (err) { | |
if (err) { | |
console.log('FAILED to process the image - ' + dest_key); | |
} else { | |
// image processed, push it to S3 bucket | |
var new_image_content = new Buffer(fs.readFileSync(newFile)); | |
var put_params = {Bucket: outbucket, Key: dest_key, Body: new_image_content, ContentType: response.ContentType}; | |
s3.putObject(put_params, function (err) { | |
if (err) | |
{ | |
console.log('Failed to upload the file - ' + dest_key); | |
console.log(err, err.stack); | |
} | |
else | |
{ | |
console.log('SUCCESS sent the processed image to S3 outbound bucket - ' + dest_key); | |
var result = {}; | |
result.dest_key = dest_key; | |
result.status = true; | |
output.push(result); | |
//Delete the created tmp file | |
try { | |
fs.unlinkSync(newFile); | |
} catch (err) { | |
console.log('FAILED to unlink the temporary file - ' + newFile); | |
} | |
console.log('DONE processing and cleanup for - ' + dest_key); | |
} | |
}); | |
} | |
}); | |
} catch (err) { | |
console.log('ERROR - GraphicsMagik operation failed:', err); | |
} | |
} | |
}); | |
} | |
//----------------------------------------------------------------------------- | |
exports.handler = (event, context, callback) => { | |
// DEBUGGING | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
console.log('Exec Env =' + process.env.AWS_EXECUTION_ENV); | |
console.log('remaining time =', context.getRemainingTimeInMillis()); | |
console.log('functionName =', context.functionName); | |
console.log('AWSrequestID =', context.awsRequestId); | |
console.log('logGroupName =', context.logGroupName); | |
console.log('logStreamName =', context.logStreamName); | |
console.log('Entered handler function will dump into ' + outbucket); | |
var image_key = event.Records[0].s3.object.key; | |
process_image(image_key); | |
callback(null, output); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment