From 2d22573588538cf227565b6b0a139bf40db5f891 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Fri, 8 Nov 2019 17:44:04 +0100 Subject: [PATCH 1/3] Choose your endpoint. See https://github.com/mozilla/send/issues/1239 --- server/storage/s3.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/storage/s3.js b/server/storage/s3.js index bb2b0100..271fa605 100644 --- a/server/storage/s3.js +++ b/server/storage/s3.js @@ -1,4 +1,12 @@ const AWS = require('aws-sdk'); +const config = {}; +if (typeof process.env.AWS_S3_ENDPOINT !== 'undefined') { + config['endpoint'] = process.env.AWS_S3_ENDPOINT; +} +if (typeof process.env.AWS_S3_USE_PATH_STYLE_ENDPOINT !== 'undefined') { + config['s3ForcePathStyle'] = process.env.AWS_S3_USE_PATH_STYLE_ENDPOINT == 'true' ? true : false; +} +AWS.config.update(config); const s3 = new AWS.S3(); class S3Storage { From 97deb78de6a4f16fea71d3103cf6fdfc49527543 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Fri, 8 Nov 2019 17:58:33 +0100 Subject: [PATCH 2/3] Use the config object. --- server/config.js | 10 ++++++++++ server/storage/s3.js | 26 ++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/server/config.js b/server/config.js index 72b750ea..8a9ef742 100644 --- a/server/config.js +++ b/server/config.js @@ -9,6 +9,16 @@ const conf = convict({ default: '', env: 'S3_BUCKET' }, + s3_endpoint: { + format: String, + default: '', + env: 'S3_ENDPOINT' + }, + s3_use_path_style_endpoint: { + format: Boolean, + default: false, + env: 'S3_USE_PATH_STYLE_ENDPOINT' + }, gcs_bucket: { format: String, default: '', diff --git a/server/storage/s3.js b/server/storage/s3.js index 271fa605..e3553889 100644 --- a/server/storage/s3.js +++ b/server/storage/s3.js @@ -1,33 +1,31 @@ const AWS = require('aws-sdk'); -const config = {}; -if (typeof process.env.AWS_S3_ENDPOINT !== 'undefined') { - config['endpoint'] = process.env.AWS_S3_ENDPOINT; -} -if (typeof process.env.AWS_S3_USE_PATH_STYLE_ENDPOINT !== 'undefined') { - config['s3ForcePathStyle'] = process.env.AWS_S3_USE_PATH_STYLE_ENDPOINT == 'true' ? true : false; -} -AWS.config.update(config); -const s3 = new AWS.S3(); class S3Storage { constructor(config, log) { this.bucket = config.s3_bucket; this.log = log; + const cfg = {}; + if (config.s3_endpoint != '') { + cfg['endpoint'] = config.s3_endpoint; + } + cfg['s3ForcePathStyle'] = config.s3_use_path_style_endpoint + AWS.config.update(cfg); + this.s3 = new AWS.S3(); } async length(id) { - const result = await s3 + const result = await this.s3 .headObject({ Bucket: this.bucket, Key: id }) .promise(); return result.ContentLength; } getStream(id) { - return s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream(); + return this.s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream(); } set(id, file) { - const upload = s3.upload({ + const upload = this.s3.upload({ Bucket: this.bucket, Key: id, Body: file @@ -37,11 +35,11 @@ class S3Storage { } del(id) { - return s3.deleteObject({ Bucket: this.bucket, Key: id }).promise(); + return this.s3.deleteObject({ Bucket: this.bucket, Key: id }).promise(); } ping() { - return s3.headBucket({ Bucket: this.bucket }).promise(); + return this.s3.headBucket({ Bucket: this.bucket }).promise(); } } From 5a70362b7922b12fb87b692ac9cafe5398fe4aa1 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Fri, 8 Nov 2019 18:11:26 +0100 Subject: [PATCH 3/3] Fix: AWS.config is a stub. --- test/backend/s3-tests.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/backend/s3-tests.js b/test/backend/s3-tests.js index 997b7c34..9e6642fd 100644 --- a/test/backend/s3-tests.js +++ b/test/backend/s3-tests.js @@ -22,6 +22,9 @@ const s3Stub = { }; const awsStub = { + config: { + update: sinon.stub() + }, S3: function() { return s3Stub; }