diff --git a/packages/@aws-cdk/aws-s3/lib/notifications-resource/notifications-resource.ts b/packages/@aws-cdk/aws-s3/lib/notifications-resource/notifications-resource.ts index 833e58343e4a2..602c5a29b4433 100644 --- a/packages/@aws-cdk/aws-s3/lib/notifications-resource/notifications-resource.ts +++ b/packages/@aws-cdk/aws-s3/lib/notifications-resource/notifications-resource.ts @@ -122,6 +122,8 @@ function renderFilters(filters?: NotificationKeyFilter[]): Filter | undefined { } const renderedRules = new Array(); + let hasPrefix = false; + let hasSuffix = false; for (const rule of filters) { if (!rule.suffix && !rule.prefix) { @@ -129,11 +131,19 @@ function renderFilters(filters?: NotificationKeyFilter[]): Filter | undefined { } if (rule.suffix) { + if (hasSuffix) { + throw new Error('Cannot specify more than one suffix rule in a filter.'); + } renderedRules.push({ Name: 'suffix', Value: rule.suffix }); + hasSuffix = true; } if (rule.prefix) { + if (hasPrefix) { + throw new Error('Cannot specify more than one prefix rule in a filter.'); + } renderedRules.push({ Name: 'prefix', Value: rule.prefix }); + hasPrefix = true; } } diff --git a/packages/@aws-cdk/aws-s3/test/test.notification.ts b/packages/@aws-cdk/aws-s3/test/test.notification.ts index 87cf07d59e990..41dcb1f1e4856 100644 --- a/packages/@aws-cdk/aws-s3/test/test.notification.ts +++ b/packages/@aws-cdk/aws-s3/test/test.notification.ts @@ -21,4 +21,76 @@ export = { test.done(); }, -}; \ No newline at end of file + + 'can specify prefix and suffix filter rules'(test: Test) { + const stack = new cdk.Stack(); + + const bucket = new s3.Bucket(stack, 'MyBucket'); + + bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { + bind: () => ({ + arn: 'ARN', + type: s3.BucketNotificationDestinationType.TOPIC + }), + }, { prefix: 'images/', suffix: '.png' }); + + expect(stack).to(haveResource('Custom::S3BucketNotifications', { + NotificationConfiguration: { + TopicConfigurations: [ + { + Events: [ + 's3:ObjectCreated:*' + ], + Filter: { + Key: { + FilterRules: [ + { + Name: 'suffix', + Value: '.png' + }, + { + Name: 'prefix', + Value: 'images/' + } + ] + } + }, + TopicArn: 'ARN' + } + ] + } + })); + + test.done(); + }, + + 'throws with multiple prefix rules in a filter'(test: Test) { + const stack = new cdk.Stack(); + + const bucket = new s3.Bucket(stack, 'MyBucket'); + + test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { + bind: () => ({ + arn: 'ARN', + type: s3.BucketNotificationDestinationType.TOPIC + }), + }, { prefix: 'images/'}, { prefix: 'archive/' }), /prefix rule/); + + test.done(); + }, + + 'throws with multiple suffix rules in a filter'(test: Test) { + const stack = new cdk.Stack(); + + const bucket = new s3.Bucket(stack, 'MyBucket'); + + test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { + bind: () => ({ + arn: 'ARN', + type: s3.BucketNotificationDestinationType.TOPIC + }), + }, { suffix: '.png'}, { suffix: '.zip' }), /suffix rule/); + + test.done(); + } +};