-
-
Save eddywashere/4178099 to your computer and use it in GitHub Desktop.
deploy to s3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.registerMultiTask('deploy', 'deploy program to cloud', function() { | |
var pkgcloud = require('pkgcloud') | |
, walk = require('walk') | |
, fs = require('fs') | |
, done = this.async() | |
, self = this; | |
grunt.log.writeln('Prepare upload to cloud (Prepare deploy configuration)'); | |
var content, amazon; | |
try { | |
content = JSON.parse(fs.readFileSync('./s3.json')); | |
amazon = pkgcloud.storage.createClient({ | |
provider: 'amazon', | |
key: content.secret, | |
keyId: content.key | |
}); | |
} catch (e) { | |
grunt.fail.fatal('Can not get s3 key to deploy files'); | |
} | |
var walker = walk.walk(self.data.from); | |
walker.on('file', function(root, fileStat, next) { | |
var readpath = root + '/' + fileStat.name; | |
var prefix = root.substr((self.data.from.length + 1)); | |
var filepath = ''; | |
if (prefix === '') { | |
filepath = fileStat.name; | |
} else { | |
filepath = prefix + '/' + fileStat.name; | |
} | |
fs.createReadStream(readpath).pipe(amazon.upload({ | |
container: self.data.to, | |
remote: filepath | |
}, function(err) { | |
if (err) { | |
grunt.log.error(readpath + ' upload failed!'); | |
} else { | |
grunt.log.error('Upload from ' + readpath); | |
grunt.log.error('to ' + filepath); | |
} | |
next(); | |
})); | |
}); | |
walker.on('errors', function(root, fileStat, next) { | |
grunt.log.error('Can not read ' + fileStat.name + ' correctly'); | |
next(); | |
}); | |
walker.on('end', function() { | |
grunt.log.error('Deploy all programs to cloud'); | |
done(); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deploy: { | |
from: 'FILE_PATH', | |
to: 'S3_BUCKET' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment