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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ class S3Adapter {
}

if (!this._baseUrl) {
return `https://${this._bucket}.s3.amazonaws.com/${fileKey}`;
// The region has to be in the host. The global endpoint only reaches
// buckets in regions that are enabled by default, so an opt-in region
// such as ap-east-1 is unreachable without it.
return `https://${this._bucket}.s3.${this._region}.amazonaws.com/${fileKey}`;
}

const baseUrlFileKey = this._baseUrlDirect ? fileName : fileKey;
Expand Down
1 change: 1 addition & 0 deletions lib/optionsFromArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const optionsFromArguments = function optionsFromArguments(args) {

if (otherOptions) {
options.bucketPrefix = otherOptions.bucketPrefix;
options.region = otherOptions.region;
options.credentials = otherOptions.credentials;
options.directAccess = otherOptions.directAccess;
options.fileAcl = otherOptions.fileAcl;
Expand Down
38 changes: 35 additions & 3 deletions spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ describe('S3Adapter tests', () => {
expect(options.bucket).toEqual('bucket');
});

it('should accept a region alongside a bucket string', () => {
const args = ['bucket', { region: 'ap-east-1' }];
const options = optionsFromArguments(args);
expect(options.region).toEqual('ap-east-1');
});

it('should accept a region alongside key, secret and bucket', () => {
const args = ['key', 'secret', 'bucket', { region: 'ap-east-1' }];
const options = optionsFromArguments(args);
expect(options.region).toEqual('ap-east-1');
});

it('should still default the region when the options object omits it', () => {
const args = ['key', 'secret', 'bucket', { bucketPrefix: 'test/' }];
const options = optionsFromArguments(args);
expect(options.region).toEqual('us-east-1');
});

it('should accept key, secret, bucket, and options object as args', () => {
const confObj = { bucketPrefix: 'test/' };
const args = ['key', 'secret', 'bucket', confObj];
Expand Down Expand Up @@ -489,7 +507,21 @@ describe('S3Adapter tests', () => {
delete options.baseUrl;
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
'https://my-bucket.s3.amazonaws.com/foo/bar/test.png'
'https://my-bucket.s3.us-east-1.amazonaws.com/foo/bar/test.png'
);
});

it('should address an opt-in region directly', async () => {
// The global endpoint does not reach buckets in regions that are not
// enabled by default, so the region has to be in the host.
const s3 = new S3Adapter({
bucket: 'my-bucket',
region: 'ap-east-1',
directAccess: true,
bucketPrefix: 'foo/bar/',
});
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
'https://my-bucket.s3.ap-east-1.amazonaws.com/foo/bar/test.png'
);
});
});
Expand Down Expand Up @@ -540,7 +572,7 @@ describe('S3Adapter tests', () => {
delete options.baseUrl;
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
'https://my-bucket.s3.amazonaws.com/foo/bar/test.png'
'https://my-bucket.s3.us-east-1.amazonaws.com/foo/bar/test.png'
);
});
});
Expand Down Expand Up @@ -616,7 +648,7 @@ describe('S3Adapter tests', () => {
delete options.baseUrl;
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
'https://my-bucket.s3.amazonaws.com/foo/bar/test.png'
'https://my-bucket.s3.us-east-1.amazonaws.com/foo/bar/test.png'
);
});

Expand Down