Skip to content

Commit

Permalink
Add s3overrides options format
Browse files Browse the repository at this point in the history
  • Loading branch information
dsix-work committed Oct 3, 2016
1 parent bb933cc commit 7fbe63f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ S3Adapter("bucket", options)
S3Adapter("key", "secret", "bucket")
S3Adapter("key", "secret", "bucket", options)
S3Adapter(options) // where options must contain bucket.
S3Adapter(options, s3overrides)
```
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).

In this form if you set `s3overrides.params`, you must set at least `s3overrides.params.Bucket`

or with an options hash

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function S3Adapter() {
s3Options.secretAccessKey = options.secretKey;
}

Object.assign(s3Options, options.s3overrides);

this._s3Client = new AWS.S3(s3Options);
this._hasBucket = false;
}
Expand Down
12 changes: 11 additions & 1 deletion lib/optionsFromArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function fromEnvironmentOrDefault(options, key, env, defaultValue) {
const optionsFromArguments = function optionsFromArguments(args) {
const stringOrOptions = args[0];
let options = {};
let s3overrides = {};
let otherOptions;

if (typeof stringOrOptions == 'string') {
Expand Down Expand Up @@ -48,7 +49,15 @@ const optionsFromArguments = function optionsFromArguments(args) {
options.globalCacheControl = otherOptions.globalCacheControl;
}
} else {
options = stringOrOptions || {};
if (args.length == 1) {
options = stringOrOptions || {};
} else if (args.length == 2) {
options = stringOrOptions;
s3overrides = args[1];
options.bucket = s3overrides.params.Bucket;
} else {
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
}
}
options = requiredOrFromEnvironment(options, 'bucket', 'S3_BUCKET');
options = fromEnvironmentOrDefault(options, 'accessKey', 'S3_ACCESS_KEY', null);
Expand All @@ -61,6 +70,7 @@ const optionsFromArguments = function optionsFromArguments(args) {
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
options = fromEnvironmentOrDefault(
options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
options.s3overrides = s3overrides;

return options;
}
Expand Down
16 changes: 15 additions & 1 deletion spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('S3Adapter tests', () => {
it('should throw when not initialized properly', () => {
expect(() => {
var s3 = new S3Adapter();
}).toThrow("S3Adapter requires option 'bucket' or env. variable S3_BUCKET")
}).toThrow(new Error('Failed to configure S3Adapter. Arguments don\'t make sense'));

expect(() =>  {
var s3 = new S3Adapter('accessKey', 'secretKey', {});
Expand All @@ -28,6 +28,10 @@ describe('S3Adapter tests', () => {
expect(() => {
var s3 = new S3Adapter({ bucket: 'bucket'});
}).not.toThrow()

expect(() => {
var s3 = new S3Adapter({}, { params:{ Bucket: 'bucket'}});
}).not.toThrow()
});

describe('to find the right arg in the right place', () => {
Expand Down Expand Up @@ -62,6 +66,16 @@ describe('S3Adapter tests', () => {
expect(options.bucket).toEqual('bucket');
expect(options.bucketPrefix).toEqual('test/');
});

it('should accept options and overrides as args', () => {
var confObj = { bucketPrefix: 'test/', bucket: 'bucket-1', secretKey: 'secret-1', accessKey: 'key-1' };
var overridesObj = { secretAccessKey: 'secret-2', accessKeyId: 'key-2', params: { Bucket: 'bucket-2' }};
var s3 = new S3Adapter(confObj, overridesObj);
expect(s3._s3Client.config.accessKeyId).toEqual('key-2');
expect(s3._s3Client.config.secretAccessKey).toEqual('secret-2');
expect(s3._s3Client.config.params.Bucket).toEqual('bucket-2');
expect(s3._bucketPrefix).toEqual('test/');
});
});

describe('getFileLocation', () => {
Expand Down

0 comments on commit 7fbe63f

Please sign in to comment.