Skip to content

Instantly share code, notes, and snippets.

@chilts
Forked from twhid/gist:2648062
Created September 9, 2012 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chilts/3687910 to your computer and use it in GitHub Desktop.
Save chilts/3687910 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({
accessKeyId : aws.accessKey,
secretAccessKey : aws.secretKey,
accountId : aws.accountId ,
region : 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();
}
});
});
});
@chilts
Copy link
Author

chilts commented Sep 9, 2012

I've just forked and changed the logic for creating a new S3 client object. The syntax changed in a later version of AwsSum so it's just an update really, haven't changed anything else.

Thanks for showing this grunt task. I may have to use it. :)

Cheers,
Andy (author of AwsSum)

@dustMason
Copy link

FYI: I just reworked this code, updated it for the current versions of grunt and awssum and put it up as an npm package for all to use https://github.com/dustMason/grunt-awssum-deploy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment