From 9376e70a9249ee8846e62daa980418f3d3c55d97 Mon Sep 17 00:00:00 2001 From: walrusautotune Date: Sat, 5 Nov 2022 23:05:01 -0400 Subject: [PATCH] fix(aws-s3): Unable to use Bucket.fromBucketName on legacy s3 bucket with underscore in the name --- packages/@aws-cdk/aws-s3/lib/bucket.ts | 4 ++-- packages/@aws-cdk/aws-s3/test/bucket.test.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-s3/lib/bucket.ts b/packages/@aws-cdk/aws-s3/lib/bucket.ts index 60c5498a33794..b4deb768eaee6 100644 --- a/packages/@aws-cdk/aws-s3/lib/bucket.ts +++ b/packages/@aws-cdk/aws-s3/lib/bucket.ts @@ -1724,9 +1724,9 @@ export class Bucket extends BucketBase { if (bucketName.length < 3 || bucketName.length > 63) { errors.push('Bucket name must be at least 3 and no more than 63 characters'); } - const charsetMatch = bucketName.match(/[^a-z0-9.-]/); + const charsetMatch = bucketName.match(/[^a-z0-9_.-]/); if (charsetMatch) { - errors.push('Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) ' + errors.push('Bucket name must only contain lowercase characters and the symbols, period (.), underscore(_), and dash (-) ' + `(offset: ${charsetMatch.index})`); } if (!/[a-z0-9]/.test(bucketName.charAt(0))) { diff --git a/packages/@aws-cdk/aws-s3/test/bucket.test.ts b/packages/@aws-cdk/aws-s3/test/bucket.test.ts index ed727d615578a..49a544072c6bc 100644 --- a/packages/@aws-cdk/aws-s3/test/bucket.test.ts +++ b/packages/@aws-cdk/aws-s3/test/bucket.test.ts @@ -107,7 +107,7 @@ describe('bucket', () => { const expectedErrors = [ `Invalid S3 bucket name (value: ${bucket})`, 'Bucket name must be at least 3 and no more than 63 characters', - 'Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 5)', + 'Bucket name must only contain lowercase characters and the symbols, period (.), underscore(_), and dash (-) (offset: 5)', 'Bucket name must start and end with a lowercase character or number (offset: 0)', `Bucket name must start and end with a lowercase character or number (offset: ${bucket.length - 1})`, 'Bucket name must not have dash next to period, or period next to dash, or consecutive periods (offset: 7)',