-
-
Save chilts/3687910 to your computer and use it in GitHub Desktop.
grunt task to deploy JS files to S3 with awssum
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('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(); | |
} | |
}); | |
}); | |
}); |
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
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)