Skip to content

Instantly share code, notes, and snippets.

@junajan
Created October 17, 2015 00:50
Show Gist options
  • Select an option

  • Save junajan/0e8b9e013e5390424a50 to your computer and use it in GitHub Desktop.

Select an option

Save junajan/0e8b9e013e5390424a50 to your computer and use it in GitHub Desktop.
var async = require('async');
var _ = require('lodash');
var fs = require('fs');
var zlib = require('zlib');
var AWS = require('aws-sdk');
var mime = require('mime');
function Bucket(info, config) {
var self = this;
self.info = info;
AWS.config.update(config.keys);
AWS.config.update({region: config.region});
var s3 = new AWS.S3({params: {Bucket: config.bucket}});
this.saveStream = function(name, contentType, stream, done) {
s3.upload({Key: name, Body: stream, ACL: 'public-read', ContentType: contentType}, function(err, data) {
return done(err, data);
});
};
this.save = function(from, name, done) {
var rstream = fs.createReadStream(from);
self.saveStream(name, mime.lookup(from), rstream, done);
};
this.read = function(name, done) {
console.log('Reading ... ', name);
done(s3.getObject({Key: name}).createReadStream());
};
console.log('Inited AWS bucket storage');
return this;
}
var obj = null;
module.exports = function(info, config) {
if(obj) return obj;
return obj = new Bucket(info, config);
};
console.log('Loaded AWS bucket storage');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment