Created
May 4, 2015 17:13
-
-
Save h2non/b30e7e6114415cbef172 to your computer and use it in GitHub Desktop.
Simple example demostrating how to wrap the S3.putObject() in AWS node.js SDK to transform it into a pipeable compatible interface. Related issue: https://github.com/aws/aws-sdk-js/issues/588
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 fs = require('fs') | |
var pipefy = require('pipefy') | |
var AWS = require('aws-sdk') | |
var s3 = new AWS.S3() | |
// Keep safe the original function with proper scope | |
var putObject = s3.putObject.bind(s3) | |
// Override the function | |
s3.putObject = function (opts, cb) { | |
if (!opts.Body) { | |
return pipefy(mapBody) | |
} | |
return putObject(opts, cb) | |
function mapBody(buffer) { | |
opts.Body = buffer | |
putObject(opts, cb) | |
} | |
} | |
var opts = { | |
Bucket: 'example', | |
Key: 'package.json', | |
ContentType: 'application/json' | |
} | |
// Read a file via as stream and pipe it to S3 | |
fs.createReadStream('package.json') | |
.pipe(s3.putObject(opts, handler)) | |
function handler(err, data) { | |
if (!err) { | |
console.log('Uploaded!') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment