fix: fix parsing of S3 URLs for the default region

Closes: #11913
Signed-off-by: Patrick Jungermann <Patrick.Jungermann@gmail.com>
This commit is contained in:
Patrick Jungermann
2022-06-15 14:25:19 +02:00
parent 2bb51571de
commit b1edb5cfd9
3 changed files with 19 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Fix parsing of S3 URLs for the default region.
@@ -39,6 +39,15 @@ const treeResponseFactory = DefaultReadTreeResponseFactory.create({
describe('parseUrl', () => {
it('supports all aws formats', () => {
expect(
parseUrl('https://s3.amazonaws.com/my.bucket-3/a/puppy.jpg', {
host: 'amazonaws.com',
}),
).toEqual({
path: 'a/puppy.jpg',
bucket: 'my.bucket-3',
region: 'us-east-1',
});
expect(
parseUrl('https://s3.us-west-2.amazonaws.com/my.bucket-3/a/puppy.jpg', {
host: 'amazonaws.com',
@@ -35,6 +35,8 @@ import { ForwardedError, NotModifiedError } from '@backstage/errors';
import { ListObjectsV2Output, ObjectList } from 'aws-sdk/clients/s3';
import { ReadUrlResponseFactory } from './ReadUrlResponseFactory';
const DEFAULT_REGION = 'us-east-1';
/**
* Path style URLs: https://s3.(region).amazonaws.com/(bucket)/(key)
* The region can also be on the old form: https://s3-(region).amazonaws.com/(bucket)/(key)
@@ -57,7 +59,7 @@ export function parseUrl(
// Treat Amazon hosted separately because it has special region logic
if (config.host === 'amazonaws.com') {
const match = host.match(
/^(?:([a-z0-9.-]+)\.)?s3[.-]([a-z0-9-]+)\.amazonaws\.com$/,
/^(?:([a-z0-9.-]+)\.)?s3(?:[.-]([a-z0-9-]+))?\.amazonaws\.com$/,
);
if (!match) {
throw new Error(`Invalid AWS S3 URL ${url}`);
@@ -76,14 +78,14 @@ export function parseUrl(
return {
path: pathname.substring(slashIndex + 1),
bucket: pathname.substring(0, slashIndex),
region: hostRegion,
region: hostRegion ?? DEFAULT_REGION,
};
}
return {
path: pathname,
bucket: hostBucket,
region: hostRegion,
region: hostRegion ?? DEFAULT_REGION,
};
}