@@ -2,6 +2,8 @@ import iam = require('@aws-cdk/aws-iam');
2
2
import kms = require( '@aws-cdk/aws-kms' ) ;
3
3
import cdk = require( '@aws-cdk/cdk' ) ;
4
4
import { BucketPolicy } from './bucket-policy' ;
5
+ import { IBucketNotificationDestination } from './notification-dest' ;
6
+ import { BucketNotifications } from './notifications-resource' ;
5
7
import perms = require( './perms' ) ;
6
8
import { LifecycleRule } from './rule' ;
7
9
import { BucketArn , BucketDomainName , BucketDualStackDomainName , cloudformation } from './s3.generated' ;
@@ -289,6 +291,7 @@ export class Bucket extends BucketRef {
289
291
protected autoCreatePolicy = true ;
290
292
private readonly lifecycleRules : LifecycleRule [ ] = [ ] ;
291
293
private readonly versioned ?: boolean ;
294
+ private readonly notifications : BucketNotifications ;
292
295
293
296
constructor ( parent : cdk . Construct , name : string , props : BucketProps = { } ) {
294
297
super ( parent , name ) ;
@@ -316,6 +319,10 @@ export class Bucket extends BucketRef {
316
319
317
320
// Add all lifecycle rules
318
321
( props . lifecycleRules || [ ] ) . forEach ( this . addLifecycleRule . bind ( this ) ) ;
322
+
323
+ // defines a BucketNotifications construct. Notice that an actual resource will only
324
+ // be added if there are notifications added, so we don't need to condition this.
325
+ this . notifications = new BucketNotifications ( this , 'Notifications' , { bucket : this } ) ;
319
326
}
320
327
321
328
/**
@@ -333,6 +340,53 @@ export class Bucket extends BucketRef {
333
340
this . lifecycleRules . push ( rule ) ;
334
341
}
335
342
343
+ /**
344
+ * Adds a bucket notification event destination.
345
+ * @param event The event to trigger the notification
346
+ * @param dest The notification destination (Lambda, SNS Topic or SQS Queue)
347
+ *
348
+ * @param filters S3 object key filter rules to determine which objects
349
+ * trigger this event. Each filter must include a `prefix` and/or `suffix`
350
+ * that will be matched against the s3 object key. Refer to the S3 Developer Guide
351
+ * for details about allowed filter rules.
352
+ *
353
+ * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#notification-how-to-filtering
354
+ *
355
+ * @example
356
+ *
357
+ * bucket.onEvent(EventType.OnObjectCreated, myLambda, 'home/myusername/*')
358
+ *
359
+ * @see
360
+ * https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
361
+ */
362
+ public onEvent ( event : EventType , dest : IBucketNotificationDestination , ...filters : NotificationKeyFilter [ ] ) {
363
+ this . notifications . addNotification ( event , dest , ...filters ) ;
364
+ }
365
+
366
+ /**
367
+ * Subscribes a destination to receive notificatins when an object is
368
+ * created in the bucket. This is identical to calling
369
+ * `onEvent(EventType.ObjectCreated)`.
370
+ *
371
+ * @param dest The notification destination (see onEvent)
372
+ * @param filters Filters (see onEvent)
373
+ */
374
+ public onObjectCreated ( dest : IBucketNotificationDestination , ...filters : NotificationKeyFilter [ ] ) {
375
+ return this . onEvent ( EventType . ObjectCreated , dest , ...filters ) ;
376
+ }
377
+
378
+ /**
379
+ * Subscribes a destination to receive notificatins when an object is
380
+ * removed from the bucket. This is identical to calling
381
+ * `onEvent(EventType.ObjectRemoved)`.
382
+ *
383
+ * @param dest The notification destination (see onEvent)
384
+ * @param filters Filters (see onEvent)
385
+ */
386
+ public onObjectRemoved ( dest : IBucketNotificationDestination , ...filters : NotificationKeyFilter [ ] ) {
387
+ return this . onEvent ( EventType . ObjectRemoved , dest , ...filters ) ;
388
+ }
389
+
336
390
/**
337
391
* Set up key properties and return the Bucket encryption property from the
338
392
* user's configuration.
@@ -485,6 +539,126 @@ export class S3Url extends cdk.Token {
485
539
486
540
}
487
541
542
+ /**
543
+ * Notification event types.
544
+ */
545
+ export enum EventType {
546
+ /**
547
+ * Amazon S3 APIs such as PUT, POST, and COPY can create an object. Using
548
+ * these event types, you can enable notification when an object is created
549
+ * using a specific API, or you can use the s3:ObjectCreated:* event type to
550
+ * request notification regardless of the API that was used to create an
551
+ * object.
552
+ */
553
+ ObjectCreated = 's3:ObjectCreated:*' ,
554
+
555
+ /**
556
+ * Amazon S3 APIs such as PUT, POST, and COPY can create an object. Using
557
+ * these event types, you can enable notification when an object is created
558
+ * using a specific API, or you can use the s3:ObjectCreated:* event type to
559
+ * request notification regardless of the API that was used to create an
560
+ * object.
561
+ */
562
+ ObjectCreatedPut = 's3:ObjectCreated:Put' ,
563
+
564
+ /**
565
+ * Amazon S3 APIs such as PUT, POST, and COPY can create an object. Using
566
+ * these event types, you can enable notification when an object is created
567
+ * using a specific API, or you can use the s3:ObjectCreated:* event type to
568
+ * request notification regardless of the API that was used to create an
569
+ * object.
570
+ */
571
+ ObjectCreatedPost = 's3:ObjectCreated:Post' ,
572
+
573
+ /**
574
+ * Amazon S3 APIs such as PUT, POST, and COPY can create an object. Using
575
+ * these event types, you can enable notification when an object is created
576
+ * using a specific API, or you can use the s3:ObjectCreated:* event type to
577
+ * request notification regardless of the API that was used to create an
578
+ * object.
579
+ */
580
+ ObjectCreatedCopy = 's3:ObjectCreated:Copy' ,
581
+
582
+ /**
583
+ * Amazon S3 APIs such as PUT, POST, and COPY can create an object. Using
584
+ * these event types, you can enable notification when an object is created
585
+ * using a specific API, or you can use the s3:ObjectCreated:* event type to
586
+ * request notification regardless of the API that was used to create an
587
+ * object.
588
+ */
589
+ ObjectCreatedCompleteMultipartUpload = 's3:ObjectCreated:CompleteMultipartUpload' ,
590
+
591
+ /**
592
+ * By using the ObjectRemoved event types, you can enable notification when
593
+ * an object or a batch of objects is removed from a bucket.
594
+ *
595
+ * You can request notification when an object is deleted or a versioned
596
+ * object is permanently deleted by using the s3:ObjectRemoved:Delete event
597
+ * type. Or you can request notification when a delete marker is created for
598
+ * a versioned object by using s3:ObjectRemoved:DeleteMarkerCreated. For
599
+ * information about deleting versioned objects, see Deleting Object
600
+ * Versions. You can also use a wildcard s3:ObjectRemoved:* to request
601
+ * notification anytime an object is deleted.
602
+ *
603
+ * You will not receive event notifications from automatic deletes from
604
+ * lifecycle policies or from failed operations.
605
+ */
606
+ ObjectRemoved = 's3:ObjectRemoved:*' ,
607
+
608
+ /**
609
+ * By using the ObjectRemoved event types, you can enable notification when
610
+ * an object or a batch of objects is removed from a bucket.
611
+ *
612
+ * You can request notification when an object is deleted or a versioned
613
+ * object is permanently deleted by using the s3:ObjectRemoved:Delete event
614
+ * type. Or you can request notification when a delete marker is created for
615
+ * a versioned object by using s3:ObjectRemoved:DeleteMarkerCreated. For
616
+ * information about deleting versioned objects, see Deleting Object
617
+ * Versions. You can also use a wildcard s3:ObjectRemoved:* to request
618
+ * notification anytime an object is deleted.
619
+ *
620
+ * You will not receive event notifications from automatic deletes from
621
+ * lifecycle policies or from failed operations.
622
+ */
623
+ ObjectRemovedDelete = 's3:ObjectRemoved:Delete' ,
624
+
625
+ /**
626
+ * By using the ObjectRemoved event types, you can enable notification when
627
+ * an object or a batch of objects is removed from a bucket.
628
+ *
629
+ * You can request notification when an object is deleted or a versioned
630
+ * object is permanently deleted by using the s3:ObjectRemoved:Delete event
631
+ * type. Or you can request notification when a delete marker is created for
632
+ * a versioned object by using s3:ObjectRemoved:DeleteMarkerCreated. For
633
+ * information about deleting versioned objects, see Deleting Object
634
+ * Versions. You can also use a wildcard s3:ObjectRemoved:* to request
635
+ * notification anytime an object is deleted.
636
+ *
637
+ * You will not receive event notifications from automatic deletes from
638
+ * lifecycle policies or from failed operations.
639
+ */
640
+ ObjectRemovedDeleteMarkerCreated = 's3:ObjectRemoved:DeleteMarkerCreated' ,
641
+
642
+ /**
643
+ * You can use this event type to request Amazon S3 to send a notification
644
+ * message when Amazon S3 detects that an object of the RRS storage class is
645
+ * lost.
646
+ */
647
+ ReducedRedundancyLostObject = 's3:ReducedRedundancyLostObject' ,
648
+ }
649
+
650
+ export interface NotificationKeyFilter {
651
+ /**
652
+ * S3 keys must have the specified prefix.
653
+ */
654
+ prefix ?: string ;
655
+
656
+ /**
657
+ * S3 keys must have the specified suffix.
658
+ */
659
+ suffix ?: string ;
660
+ }
661
+
488
662
class ImportedBucketRef extends BucketRef {
489
663
public readonly bucketArn : BucketArn ;
490
664
public readonly bucketName : BucketName ;
0 commit comments