Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created February 13, 2012 09:33
Show Gist options
  • Save simenbrekken/1815477 to your computer and use it in GitHub Desktop.
Save simenbrekken/1815477 to your computer and use it in GitHub Desktop.
Amazon S3 PUT test
var http = require('http'),
uri = require('url'),
mime = require('mime'),
aws2js = require('aws2js');
var settings = {
s3: {
key: 'cat',
secret: 'bus',
bucket: 'fonyfw',
path: '/photos/',
endpoint: 's3.amazonaws.com'
}
};
var s3 = aws2js.load('s3', settings.s3.key, settings.s3.secret);
s3.setBucket(settings.s3.bucket);
var url = 'http://distilleryimage1.instagram.com/27c6c570558f11e19e4a12313813ffc0_7.jpg';
var id = (+new Date());
var options = uri.parse(url);
var req = http.get(options, function(res) {
var type = res.headers['content-type'];
var path = settings.s3.path + id + '.' + mime.extension(type);
var headers = {
'content-type': type,
'content-length': res.headers['content-length'],
'x-amz-acl': 'public-read'
};
s3.putStream(path, res, false, headers, function(err, s3res) {
if (err) throw err;
var resultUrl = 'http://' + settings.s3.endpoint + '/' + settings.s3.bucket + path;
console.log('Done!');
console.log(resultUrl);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment