Skip to content

Commit

Permalink
Add S3 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
groundwater committed Dec 3, 2012
1 parent 854af0e commit 882a1ef
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ Rude can upload your assets to their appropriate servers, or distribution networ


### Amazon S3 ### Amazon S3


$ rude publish s3 $ rude publish s3://bucket


### WebDAV ### WebDAV


$ rude publish http://server/dav $ rude publish http://server/dav


### SSH ### SSH


$ rude publish user@host:/path/to/web $ rude publish git://server.com:path/to/assets






Expand Down
8 changes: 6 additions & 2 deletions bin/rude.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ program
body : file body : file
} }


db.save(hash, {}, function(err,res){ var id = hash

db.save(id, {}, function(err,res){
if(err) return Error(err) if(err) return Error(err)


db.saveAttachment(res,attachment,function(err,ok){ db.saveAttachment(res,attachment,function(err,ok){
Expand All @@ -97,6 +99,7 @@ program
}) })


}) })

}) })


program program
Expand All @@ -114,7 +117,8 @@ program
}) })


var publishers = { var publishers = {
'git': require('../lib/ssh').publish 'git': require('../lib/ssh').publish,
's3' : require('../lib/s3').publish
} }


program program
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
var fs = require('fs') var fs = require('fs')


function join(){ function join(){
var out = [] var out = []
Expand Down
56 changes: 56 additions & 0 deletions lib/s3.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
var async = require('async')
var knox = require('knox')

function publish(config,db,url){

var cons = url.split('/')
var head = cons.shift()
var tail = cons.join('/')

var AWS_S3_KEY = process.env.AWS_S3_KEY
var AWS_S3_SECRET = process.env.AWS_S3_SECRET

if(!AWS_S3_KEY) return console.error('[FAIL] Please Set AWS_S3_KEY')
if(!AWS_S3_SECRET) return console.error('[FAIL] Please Set AWS_S3_SECRET')

var opts = {
key : AWS_S3_KEY,
secret : AWS_S3_SECRET,
bucket : head,
region : 'us-west-2'
}

var client = knox.createClient(opts)

async.forEach(Object.keys(config),function(name,done){
var hash = config[name]
db.getAttachment(hash,name,function(err,attachment){

var path = "/" + hash + '/' + name
var headers = {
"Content-Length": attachment.headers["content-length"],
"Content-Type" : attachment.headers["content-type"]
}

var req = client.put(path,headers)

req.on('response',function(res){
if(res.statusCode == 200) {
console.log(' √',name)
done()
}else{
console.error('[FAIL] Failed to Upload to S3')
console.error('[FAIL] Status Code %d',res.statusCode)
done(res)
}
})
req.end(attachment.body)

})
},function(res){
console.log('Done')
})

}

module.exports.publish = publish
3 changes: 2 additions & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"commander": "latest", "commander": "latest",
"ssh2": "~0.1.6", "ssh2": "~0.1.6",
"colors": "~0.6.0-1", "colors": "~0.6.0-1",
"async": "~0.1.22" "async": "~0.1.22",
"knox": "~0.4.2"
}, },
"preferGlobal": true "preferGlobal": true
} }

0 comments on commit 882a1ef

Please sign in to comment.