Skip to content

Instantly share code, notes, and snippets.

@h2non
Created May 4, 2015 17:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h2non/b30e7e6114415cbef172 to your computer and use it in GitHub Desktop.
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
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