Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Last active September 2, 2015 15:22
Show Gist options
  • Save fujiwara/1192cc00ff67159f55a0 to your computer and use it in GitHub Desktop.
Save fujiwara/1192cc00ff67159f55a0 to your computer and use it in GitHub Desktop.
var aws = require('aws-sdk');
var fs = require('fs');
var zlib = require('zlib');
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log(JSON.stringify(context, null, 2));
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
if ( key.match(/\.gz$/) ) {
context.done(key + ' alread gzipped');
return;
}
var s3 = new aws.S3();
var gzStream = s3.getObject({ Bucket: bucket, Key: key }).
createReadStream().pipe(zlib.createGzip());
var s3obj = new aws.S3({ params: { Bucket: bucket, Key: 'gz/' + key + '.gz' }});
s3obj.upload({Body: gzStream}).
send(function(err, data) {
console.log(err, data);
if (err) {
context.done('error putting object', err);
} else {
context.done();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment