Permalink
Browse files

Add S3 Support

  • Loading branch information...
1 parent 854af0e commit 882a1efdfa587b66b41df05601f344d7e7e8d07c @groundwater committed Dec 3, 2012
Showing with 67 additions and 6 deletions.
  1. +2 −2 README.md
  2. +6 −2 bin/rude.js
  3. +1 −1 index.js
  4. +56 −0 lib/s3.js
  5. +2 −1 package.json
View
4 README.md
@@ -163,15 +163,15 @@ Rude can upload your assets to their appropriate servers, or distribution networ
### Amazon S3
- $ rude publish s3
+ $ rude publish s3://bucket
### WebDAV
$ rude publish http://server/dav
### SSH
- $ rude publish user@host:/path/to/web
+ $ rude publish git://server.com:path/to/assets
View
8 bin/rude.js
@@ -84,7 +84,9 @@ program
body : file
}
- db.save(hash, {}, function(err,res){
+ var id = hash
+
+ db.save(id, {}, function(err,res){
if(err) return Error(err)
db.saveAttachment(res,attachment,function(err,ok){
@@ -97,6 +99,7 @@ program
})
})
+
})
program
@@ -114,7 +117,8 @@ program
})
var publishers = {
- 'git': require('../lib/ssh').publish
+ 'git': require('../lib/ssh').publish,
+ 's3' : require('../lib/s3').publish
}
program
View
2 index.js
@@ -1,4 +1,4 @@
-var fs = require('fs')
+var fs = require('fs')
function join(){
var out = []
View
56 lib/s3.js
@@ -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
View
3 package.json
@@ -31,7 +31,8 @@
"commander": "latest",
"ssh2": "~0.1.6",
"colors": "~0.6.0-1",
- "async": "~0.1.22"
+ "async": "~0.1.22",
+ "knox": "~0.4.2"
},
"preferGlobal": true
}

0 comments on commit 882a1ef

Please sign in to comment.