Created
February 2, 2015 21:23
-
-
Save megafaunasoft/3c4c6fb2bd080585895c to your computer and use it in GitHub Desktop.
Using AWS Lambda and povray to render images
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
var async = require('async'); | |
var AWS = require('aws-sdk'); | |
var util = require('util'); | |
var spawn = require('child_process').spawn; | |
var fs = require('fs'); | |
var s3 = new AWS.S3(); | |
// globals | |
var dstBucket = "outrender1121" | |
exports.handler = function(event, context) { | |
console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); | |
var srcBucket = event.Records[0].s3.bucket.name; | |
var srcKey = event.Records[0].s3.object.key; | |
var dstKey = "out_" + srcKey + ".png"; | |
// Sanity checks | |
if (srcBucket == dstBucket) { | |
console.error("Destination bucket must not match source bucket.", srcBucket, dstBucket); | |
return; | |
} | |
var typeMatch = srcKey.match(/\.([^.]*)$/); | |
if (!typeMatch) { | |
console.log('skipping non-povfile: ' + srcKey); | |
return; | |
} | |
var imageType = typeMatch[1]; | |
if (imageType != "pov") { | |
console.log('skipping non-povfile: ' + srcKey); | |
return; | |
} | |
// Download the image from S3, transform, and upload to a different S3 bucket. | |
async.waterfall([ | |
// | |
// Get from S3 | |
// | |
function download(next) { | |
// Download the image from S3 into a buffer. | |
s3.getObject({ | |
Bucket: srcBucket, | |
Key: srcKey | |
}, | |
next); | |
console.log("GET "+srcKey); | |
}, | |
// | |
// Write to a file for povray -- must be in /tmp/ | |
// | |
function write_to_file(response, next) { | |
fs.writeFile("/tmp/stdin.pov", response.Body, next); | |
}, | |
// | |
// Render | |
// | |
function transform(next) { | |
var convert = spawn('./povray_37_aws', ['/tmp/stdin.pov', '-w300', '-h300', '-O-']); | |
var buffers = []; | |
convert.stdout.on('data', function(data) { | |
buffers.push(new Buffer(data, "binary")); | |
}); | |
convert.stderr.on('data', function(data) { | |
// otherwise it hangs waiting to be read | |
}); | |
convert.on('exit', function(code) { | |
console.log("exit"); | |
var allbuffer = Buffer.concat(buffers) | |
next(null, "content-type:image/PNG", allbuffer); | |
}); | |
}, | |
// | |
// Put back on S3 | |
// | |
function upload(contentType, data, next) { | |
// Stream the transformed image to a different S3 bucket. | |
s3.putObject({ | |
Bucket: dstBucket, | |
Key: dstKey, | |
Body: data, | |
ContentType: contentType | |
}, | |
next); | |
} | |
], function (err) { | |
if (err) { | |
console.error( | |
'Failed: ' + srcBucket + '/' + srcKey + ' ' + dstBucket + '/' + dstKey + | |
' Error: ' + err | |
); | |
} else { | |
console.log( | |
'Succeeded: ' + srcBucket + '/' + srcKey + ' ' + dstBucket + '/' + dstKey | |
); | |
} | |
context.done(); | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment