Skip to content

Commit

Permalink
Fix for buckets in regions other than US east
Browse files Browse the repository at this point in the history
The other regions require that you put the bucket into the endpoint (hostname). So
instead of sending the request to s3.amazonaws.com/bucketname/key you need to use
bucketname.s3.amazonaws.com/key. But the resource path that is part of the signature
is still /bucketname/key.

Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
  • Loading branch information
wereHamster authored and tj committed Apr 17, 2011
1 parent aa41cae commit 2f28de9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/knox/client.js
Expand Up @@ -31,11 +31,12 @@ var utils = require('./utils')
*/

var Client = module.exports = exports = function Client(options) {
this.host = 's3.amazonaws.com';
if (!options.key) throw new Error('aws "key" required');
if (!options.secret) throw new Error('aws "secret" required');
if (!options.bucket) throw new Error('aws "bucket" required');

this.endpoint = options.bucket + '.s3.amazonaws.com';
utils.merge(this, options);
if (!this.key) throw new Error('aws "key" required');
if (!this.secret) throw new Error('aws "secret" required');
if (!this.bucket) throw new Error('aws "bucket" required');
};

/**
Expand All @@ -49,15 +50,14 @@ var Client = module.exports = exports = function Client(options) {
*/

Client.prototype.request = function(method, filename, headers){
var options = { host: this.host, port: 80 }
, path = join('/', this.bucket, filename)
var options = { host: this.endpoint, port: 80 }
, date = new Date
, headers = headers || {};

// Default headers
utils.merge(headers, {
Date: date.toUTCString()
, Host: this.host
, Host: this.endpoint
});

// Authorization header
Expand All @@ -66,14 +66,14 @@ Client.prototype.request = function(method, filename, headers){
, secret: this.secret
, verb: method
, date: date
, resource: url.parse(path).pathname
, resource: join('/', this.bucket, filename)
, contentType: headers['Content-Type']
, amazonHeaders: auth.canonicalizeHeaders(headers)
});

// Issue request
options.method = method;
options.path = path;
options.path = join('/', filename);
options.headers = headers;
var req = http.request(options);
req.url = this.url(filename);
Expand Down Expand Up @@ -303,7 +303,7 @@ Client.prototype.deleteFile = function(filename, headers, fn){
*/

Client.prototype.url = function(filename){
return 'http://' + this.bucket + '.' + this.host + join('/', filename);
return 'http://' + join(this.endpoint, filename);
};

/**
Expand Down
13 changes: 12 additions & 1 deletion test/knox.test.js
Expand Up @@ -58,9 +58,20 @@ module.exports = {
assert.equal('foobar', client.key);
assert.equal('baz', client.secret);
assert.equal('misc', client.bucket);
assert.equal('s3.amazonaws.com', client.host);
assert.equal('misc.s3.amazonaws.com', client.endpoint);
},

'test .createClient() custom endpoint': function(assert){
var client = knox.createClient({
key: 'foobar'
, secret: 'baz'
, bucket: 'misc'
, endpoint: 's3-eu-west-1.amazonaws.com'
});

assert.equal('s3-eu-west-1.amazonaws.com', client.endpoint);
},

'test .putFile()': function(assert, done){
var n = 0;
client.putFile(jsonFixture, '/test/user.json', function(err, res){
Expand Down

0 comments on commit 2f28de9

Please sign in to comment.