Skip to content

Instantly share code, notes, and snippets.

@twhid
Created May 9, 2012 19:05
Show Gist options
  • Save twhid/2648062 to your computer and use it in GitHub Desktop.
Save twhid/2648062 to your computer and use it in GitHub Desktop.
grunt task to deploy JS files to S3 with awssum
grunt.registerMultiTask('s3deploy', 'deploy to S3 using awssum', function () {
// dependencies
var awssum = require('awssum'),
fs = require('fs'),
path = require('path'),
aws = require('./settings').aws;
var amz = awssum.load('amazon/amazon'),
AmazonS3 = awssum.load('amazon/s3'),
s3 = new AmazonS3(aws.accessKey, aws.secretKey, aws.accountId , amz.US_EAST_1),
src = this.file.src,
dest = this.file.dest,
files = fs.readdirSync(src),
deployDone = this.async(),
count = files.length,
defaults = {
BucketName: aws.bucketName,
Acl: 'public-read',
ContentType: 'text/javascript; charset=UTF-8'
};
files.forEach(function ( file ) {
var fileSrc = path.join(src, file),
body = fs.readFileSync(fileSrc, 'utf8'),
options = {
BucketName: defaults.BucketName,
Acl: defaults.Acl,
ContentType: defaults.ContentType,
ObjectName: dest + '/' + file,
ContentLength: body.length,
Body: body
};
grunt.log.writeln('Deploying ' + fileSrc);
s3.PutObject(options, function ( err, data ) {
if (err) {
grunt.log.error('AWS Error: Status Code ' + err.StatusCode);
grunt.log.error('Error Code: ' + err.Body.Error.Code);
grunt.log.error('Error Message: ' + err.Body.Error.Message);
return deployDone(false);
}
count--;
grunt.log.ok('Deployed to '+ options.BucketName + '/' + options.ObjectName +
'; Status Code ' + data.StatusCode);
if (count === 0) {
grunt.log.ok('Done deploying');
deployDone();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment