public
Last active — forked from twhid/gist:2648062

grunt task to deploy JS files to S3 with awssum

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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();
}
});
});
});

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)

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.