Skip to content

Commit e564407

Browse files
dsix-workArthur Cinader
authored andcommitted
Add s3overrides options format (#24)
1 parent bb933cc commit e564407

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ S3Adapter("bucket", options)
100100
S3Adapter("key", "secret", "bucket")
101101
S3Adapter("key", "secret", "bucket", options)
102102
S3Adapter(options) // where options must contain bucket.
103+
S3Adapter(options, s3overrides)
103104
```
105+
If you use the last form, `s3overrides` are the parameters passed to [AWS.S3](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property).
106+
107+
In this form if you set `s3overrides.params`, you must set at least `s3overrides.params.Bucket`
104108

105109
or with an options hash
106110

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ function S3Adapter() {
3434
s3Options.secretAccessKey = options.secretKey;
3535
}
3636

37+
Object.assign(s3Options, options.s3overrides);
38+
3739
this._s3Client = new AWS.S3(s3Options);
3840
this._hasBucket = false;
3941
}

lib/optionsFromArguments.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function fromEnvironmentOrDefault(options, key, env, defaultValue) {
1818
const optionsFromArguments = function optionsFromArguments(args) {
1919
const stringOrOptions = args[0];
2020
let options = {};
21+
let s3overrides = {};
2122
let otherOptions;
2223

2324
if (typeof stringOrOptions == 'string') {
@@ -48,7 +49,15 @@ const optionsFromArguments = function optionsFromArguments(args) {
4849
options.globalCacheControl = otherOptions.globalCacheControl;
4950
}
5051
} else {
51-
options = stringOrOptions || {};
52+
if (args.length == 1) {
53+
options = stringOrOptions || {};
54+
} else if (args.length == 2) {
55+
options = stringOrOptions;
56+
s3overrides = args[1];
57+
options.bucket = s3overrides.params.Bucket;
58+
} else {
59+
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
60+
}
5261
}
5362
options = requiredOrFromEnvironment(options, 'bucket', 'S3_BUCKET');
5463
options = fromEnvironmentOrDefault(options, 'accessKey', 'S3_ACCESS_KEY', null);
@@ -61,6 +70,7 @@ const optionsFromArguments = function optionsFromArguments(args) {
6170
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
6271
options = fromEnvironmentOrDefault(
6372
options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
73+
options.s3overrides = s3overrides;
6474

6575
return options;
6676
}

spec/test.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('S3Adapter tests', () => {
99
it('should throw when not initialized properly', () => {
1010
expect(() => {
1111
var s3 = new S3Adapter();
12-
}).toThrow("S3Adapter requires option 'bucket' or env. variable S3_BUCKET")
12+
}).toThrow(new Error('Failed to configure S3Adapter. Arguments don\'t make sense'));
1313

1414
expect(() =>  {
1515
var s3 = new S3Adapter('accessKey', 'secretKey', {});
@@ -28,6 +28,10 @@ describe('S3Adapter tests', () => {
2828
expect(() => {
2929
var s3 = new S3Adapter({ bucket: 'bucket'});
3030
}).not.toThrow()
31+
32+
expect(() => {
33+
var s3 = new S3Adapter({}, { params:{ Bucket: 'bucket'}});
34+
}).not.toThrow()
3135
});
3236

3337
describe('to find the right arg in the right place', () => {
@@ -62,6 +66,16 @@ describe('S3Adapter tests', () => {
6266
expect(options.bucket).toEqual('bucket');
6367
expect(options.bucketPrefix).toEqual('test/');
6468
});
69+
70+
it('should accept options and overrides as args', () => {
71+
var confObj = { bucketPrefix: 'test/', bucket: 'bucket-1', secretKey: 'secret-1', accessKey: 'key-1' };
72+
var overridesObj = { secretAccessKey: 'secret-2', accessKeyId: 'key-2', params: { Bucket: 'bucket-2' }};
73+
var s3 = new S3Adapter(confObj, overridesObj);
74+
expect(s3._s3Client.config.accessKeyId).toEqual('key-2');
75+
expect(s3._s3Client.config.secretAccessKey).toEqual('secret-2');
76+
expect(s3._s3Client.config.params.Bucket).toEqual('bucket-2');
77+
expect(s3._bucketPrefix).toEqual('test/');
78+
});
6579
});
6680

6781
describe('getFileLocation', () => {

0 commit comments

Comments
 (0)