From b4c666fbcf554b3c4d20bd80a907b43cf0aaaf22 Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Fri, 28 Jun 2019 19:09:40 +0100 Subject: [PATCH 1/2] Add an Amazon S3 document store --- README.md | 21 ++++++++++++ lib/document_stores/amazon-s3.js | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 lib/document_stores/amazon-s3.js diff --git a/README.md b/README.md index ba2bfc2..c40be18 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,27 @@ Also, you must create an `uploads` table, which will store all the data for uplo You can optionally add the `user` and `password` properties to use a user system. +### Amazon S3 + +To use [Amazon S3](https://aws.amazon.com/s3/) as a storage system, you must +install the `aws-sdk` package via npm: + +`npm install aws-sdk` + +Once you've done that, your config section should look like this: + +```json +{ + "type": "amazon-s3", + "bucket": "your-bucket-name", + "region": "us-east-1" +} +``` + +Authentication is handled automatically by the client. Check +[Amazon's documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html) +for more information. + ## Author John Crepezzi diff --git a/lib/document_stores/amazon-s3.js b/lib/document_stores/amazon-s3.js new file mode 100644 index 0000000..11dd85d --- /dev/null +++ b/lib/document_stores/amazon-s3.js @@ -0,0 +1,56 @@ +/*global require,module,process*/ + +var AWS = require('aws-sdk'); +var winston = require('winston'); + +var AmazonS3DocumentStore = function(options) { + this.expire = options.expire; + this.bucket = options.bucket; + this.client = new AWS.S3({region: options.region}); +}; + +AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire) { + var _this = this; + + var req = { + Bucket: _this.bucket, + Key: key + }; + + _this.client.getObject(req, function(err, data) { + if(err) { + callback(false); + } + else { + callback(data.Body.toString('utf-8')); + if (_this.expire && !skipExpire) { + winston.warn('amazon s3 store cannot set expirations on keys'); + } + } + }); +} + +AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire) { + var _this = this; + + var req = { + Bucket: _this.bucket, + Key: key, + Body: data, + ContentType: 'text/plain' + }; + + _this.client.putObject(req, function(err, data) { + if (err) { + callback(false); + } + else { + callback(true); + if (_this.expire && !skipExpire) { + winston.warn('amazon s3 store cannot set expirations on keys'); + } + } + }); +} + +module.exports = AmazonS3DocumentStore; From 1fff48568f9789775b8e720bf9e20c7af942c77f Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Mon, 8 Jul 2019 16:59:04 +0100 Subject: [PATCH 2/2] Document the IAM permissions --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c40be18..23ca077 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,24 @@ Once you've done that, your config section should look like this: Authentication is handled automatically by the client. Check [Amazon's documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html) -for more information. +for more information. You will need to grant your role these permissions to +your bucket: + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "s3:GetObject", + "s3:PutObject" + ], + "Effect": "Allow", + "Resource": "arn:aws:s3:::your-bucket-name-goes-here/*" + } + ] +} +``` ## Author