diff --git a/.gitignore b/.gitignore index 072dd5e..051fbf3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/ *.swp -node_modules \ No newline at end of file +node_modules +typings +jsconfig.json \ No newline at end of file diff --git a/README.md b/README.md index 5cd5006..4209ddb 100644 --- a/README.md +++ b/README.md @@ -22,16 +22,15 @@ npm install --save serverless-client-s3 "serverless-client-s3" ], "custom" : { - "client": { - "bucketName": "bucket.name.for.the.client" - } + "client": { + "bucketName": "bucket.name.for.the.client" + } } ``` * **Warning:** The plugin will overwrite any data you have in the bucket name you set above if it already exists. * **Pro Tip:** To add staging and region functionality to your client, use Serverless Variables in the bucket name: `"bucket.name.for.the.client.${stage}.${region}"` - **Third**, Create a `client/dist` folder in the root directory of your Serverless project. This is where your distribution-ready website should live. It is recommended to have a `client/src` where you'll be developing your website, and a build script that outputs to `client/dist`. The plugin simply expects and uploads the entire `client/dist` folder to S3, configure the bucket to host the website, and make it publicly available. Or just copy/run the following commands in the root directory of your Serverless project to get a quick sample website for deployment: @@ -51,3 +50,47 @@ sls client deploy ``` **Fifth**, Have fun! + +**Sixth**, Have more fun with custom redirection, tagging and policy options. These are optional. + +```js +"plugins": [ + "serverless-client-s3" +], +"custom" : { + "client": { + "bucketName": "bucket.name.for.the.client" + }, + "Redirection" : { + "Index": "index.html", + "Path": "/MyAppPath" + }, + "Tagging": { + "TagSet": [ + { + "Key": "Region", "Value": "${region}" + } + ] + }, + "Policy": { + "Version": "2008-10-17", + "Id": "Policy1392681112290", + "Statement": [ + { + "Sid": "Stmt1392681101677", + "Effect": "Allow", + "Principal": { + "AWS": "*" + }, + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::${stage}.zzzzzzz.com", + "Condition": { + "IpAddress": { + "aws:SourceIp": "xx.yy.zzz.uuu" + } + } + } + ] + } +} +``` \ No newline at end of file diff --git a/index.js b/index.js index 79b132b..4ea27d5 100644 --- a/index.js +++ b/index.js @@ -114,6 +114,12 @@ module.exports = function(S) { _this.bucketName = populatedProject.custom.client.bucketName; _this.clientPath = path.join(_this.project.getRootPath(), 'client', 'dist'); + _this.Tagging = populatedProject.custom.client.Tagging; + _this.Policy = populatedProject.custom.client.Policy; + if (populatedProject.custom.client.Redirection) { + _this.RedirectionIndex = populatedProject.custom.client.Redirection.Index; + _this.RedirectionPath = populatedProject.custom.client.Redirection.Path; + } return BbPromise.resolve(); } @@ -187,6 +193,17 @@ module.exports = function(S) { }; return _this.aws.request('S3', 'putBucketWebsite', params, _this.evt.options.stage, _this.evt.options.region) }) + .then(function(){ + if (!_this.Tagging) return BbPromise.resolve(); + + S.utils.sDebug(`Configuring TAGs for bucket ${_this.bucketName}...`); + + let params = { + Bucket: _this.bucketName, + Tagging: _this.Tagging + }; + return _this.aws.request('S3', 'putBucketTagging', params, _this.evt.options.stage, _this.evt.options.region) + }) .then(function(){ S.utils.sDebug(`Configuring policy for bucket ${_this.bucketName}...`); @@ -206,6 +223,9 @@ module.exports = function(S) { } ] }; + if (_this.Policy) { + policy = _this.Policy; + } let params = { Bucket: _this.bucketName, @@ -253,7 +273,13 @@ module.exports = function(S) { Body: fileBuffer, ContentType: mime.lookup(filePath) }; - + + if (_this.RedirectionIndex && _this.RedirectionPath) { + let srcToken = '/dist/' + _this.RedirectionIndex; + if (filePath.indexOf(srcToken) > -1) { + params.WebsiteRedirectLocation = _this.RedirectionPath; + } + } // TODO: remove browser caching return _this.aws.request('S3', 'putObject', params, _this.evt.options.stage, _this.evt.options.region) }); diff --git a/package.json b/package.json index c9d8c6e..62c6a44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-client-s3", - "version": "2.0.0", + "version": "2.0.5", "engines": { "node": ">=4.0" },