var AWS = require('aws-sdk'),
    _ = require('underscore');

var s3Settings = {};
var env = process.env;
s3Settings.accessKeyId= env.AWS_ACCESS_KEY_ID;
s3Settings.secretAccessKey = env.AWS_SECRET_ACCESS_KEY;
s3Settings.bucket = env.AWS_S3_BUCKET;

AWS.config.update(s3Settings);
var S3 = new AWS.S3();

function parseFname( fname ){
    if (fname[0] != '/') fname = '/' + fname;
    var parts = fname.split('/');
    var bucketName = parts.splice(0,2)[1];
    var filename = parts.join('/');
    
    var args = {
        Bucket: bucketName,
        Key: filename
    };
    return args;
}

var Fs = {};

Fs.configure =AWS.config.update;
	

Fs.exists = function(fname, cb){
    var args = parseFname( fname );
    S3.headObject( args, function(err, data){
        // console.log( arguments );
        if (err) return cb();
        return cb(1);
    });
};

Fs.readFile = function( fname, encoding, cb){
    if (arguments.length == 2){
        cb = encoding;
        encoding = null;
    }
    var args = parseFname( fname );
    S3.getObject( args, function(err, data){
        if(err) return cb(err);
        var out = encoding? data.Body.toString(encoding): data.Body;
        return cb(null, out );
    });
};

Fs.readdir = function( fname, cb ){
    var args = parseFname(fname);
    args.Prefix = args.Key;
    delete args.Key;

    S3.listObjects( args, function(err, data){
        if (err) return cb(err);
        var out = [];
        data.Contents.forEach(function(content){
            out.push( content.Key.substr(args.Prefix.length+1));
        });
        _.pluck( data.Contents, 'Key');
        return cb(null, out);
    });
};

Fs.outputFile = function( fname, data, cb ){
    var args = parseFname(fname);
    args.ACL = 'private';
    args.Body = data;
    
    S3.putObject(args, function(err, res){
        if(err) return cb(err);
        return cb(null);
    });
};

module.exports = Fs;

@@ -245,62 +245,104 @@
 ET;%0A
-%0AAWS.config.update(s3Settings);%0Avar S3 = new AWS.S3();
+s3Settings.endpoint = env.AWS_S3_ENDPOINT;%0As3Settings.useSsl = env.AWS_S3_USE_SSL? true: false;%0A
 %0A%0Afu
@@ -631,54 +631,319 @@
 %0A%7D%0A%0A
-var Fs = %7B%7D;%0A%0AFs.configure =AWS.config.update;
+function parseConfig( data )%7B%0A%0A    if(data.endpoint)%7B%0A        var endpoint = new AWS.Endpoint( data.endpoint );%0A        data.endpoint = endpoint;%0A    %7D%0A    return data;%0A%7D%0A%0A%0Avar Fs = %7B%7D;%0Avar S3 = new AWS.S3( parseConfig(s3Settings) );%0A%0AFs.configure = function(data)%7B%0A    S3.config.update( parseConfig( data) );%0A%7D
 %0A%09%0A%0A

use service specific configuration instead of global configuration