Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand All @@ -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);

Expand Down
26 changes: 26 additions & 0 deletions test/createClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@ 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' }); },
/aws "secret" required/
);
});

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' }); },
Expand Down Expand Up @@ -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({
Expand Down