From 9606f63108ee9b3b0a66b452529758fe32f064f4 Mon Sep 17 00:00:00 2001 From: Frank Gasser Date: Thu, 18 Jun 2015 18:20:20 +0200 Subject: [PATCH 1/2] allow accessKeyId & secretAccessKey allow accessKeyId as key and secretAccessKey as secret in opts (like the official AWSJavaScriptSDK). --- lib/client.js | 12 ++++++++++-- test/createClient.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index ea2b3ed..6e6c8b1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -173,8 +173,8 @@ function getCopyHeaders(sourceBucket, sourceFilename, headers) { */ var Client = module.exports = exports = function Client(options) { - if (!options.key) throw new Error('aws "key" required'); - if (!options.secret) throw new Error('aws "secret" required'); + if (!options.key && !options.accessKeyId ) throw new Error('aws "key" required'); + if (!options.secret && !options.secretAccessKey) throw new Error('aws "secret" required'); if (!options.bucket) throw new Error('aws "bucket" required'); if (options.style && options.style !== 'virtualHosted' && @@ -193,6 +193,14 @@ var Client = module.exports = exports = function Client(options) { throw new Error('Bucket name "' + options.bucket + '" ' + invalidness + '.'); } + //overwrite for internal use, see official AWSJavaScriptSDK + if(options.accessKeyId) { + options.key = options.accessKeyId; + } + if(options.secretAccessKey) { + options.secret = options.secretAccessKey; + } + // Save original options, we will need them for Client#copyTo this.options = utils.merge({}, options); diff --git a/test/createClient.test.js b/test/createClient.test.js index d57a0e9..69b0e30 100644 --- a/test/createClient.test.js +++ b/test/createClient.test.js @@ -12,6 +12,13 @@ describe('knox.createClient()', function () { ); }); + it('should not ask for a key when accessKeyId is passed', function () { + assert.throws( + function () { knox.createClient({accessKeyId: 'foo'}); }, + /aws "secret" required/ + ); + }); + it('should ask for a secret when only a key is passed', function () { assert.throws( function () { knox.createClient({ key: 'foo' }); }, @@ -19,6 +26,13 @@ describe('knox.createClient()', function () { ); }); + it('should not ask for a secret when secretAccessKey is passed', function () { + assert.throws( + function () { knox.createClient({ accessKeyId: 'foo', secretAccessKey: 'bar' }); }, + /aws "bucket" required/ + ); + }); + it('should ask for a bucket when only a key and secret are passed', function () { assert.throws( function () { knox.createClient({ key: 'foo', secret: 'bar' }); }, @@ -196,6 +210,18 @@ describe('knox.createClient()', function () { assert.equal(client.bucket, 'misc'); }); + it('should copy over alias properties', function () { + var client = knox.createClient({ + accessKeyId: 'foobar' + , secretAccessKey: 'baz' + , bucket: 'misc' + }); + + assert.equal(client.key, 'foobar'); + assert.equal(client.secret, 'baz'); + assert.equal(client.bucket, 'misc'); + }); + describe('with virtual hosted style', function () { it('should use a default region and endpoint given a bucket', function () { var client = knox.createClient({ From 6959c02bf6cef41e9da2885293cf32af1142d6a8 Mon Sep 17 00:00:00 2001 From: Frank Gasser Date: Fri, 19 Jun 2015 14:28:29 +0200 Subject: [PATCH 2/2] update readme --- Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Readme.md b/Readme.md index 6d35d57..89a4ff4 100644 --- a/Readme.md +++ b/Readme.md @@ -266,6 +266,15 @@ var userDataUrl = client.https('/user.json'); Besides the required `key`, `secret`, and `bucket` options, you can supply any of the following: + +### `accessKeyId` + +Alias to `key` + +### `secretAccessKey` + +Alias to `secret` + ### `endpoint` By default knox will send all requests to the global endpoint