@@ -632,6 +632,70 @@ export interface BucketMetrics {
632
632
readonly tagFilters ?: { [ tag : string ] : any } ;
633
633
}
634
634
635
+ /**
636
+ * All http request methods
637
+ */
638
+ export enum HttpMethods {
639
+ /**
640
+ * The GET method requests a representation of the specified resource.
641
+ */
642
+ GET = "GET" ,
643
+ /**
644
+ * The PUT method replaces all current representations of the target resource with the request payload.
645
+ */
646
+ PUT = "PUT" ,
647
+ /**
648
+ * The HEAD method asks for a response identical to that of a GET request, but without the response body.
649
+ */
650
+ HEAD = "HEAD" ,
651
+ /**
652
+ * The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
653
+ */
654
+ POST = "POST" ,
655
+ /**
656
+ * The DELETE method deletes the specified resource.
657
+ */
658
+ DELETE = "DELETE" ,
659
+ }
660
+
661
+ /**
662
+ * Specifies a cross-origin access rule for an Amazon S3 bucket.
663
+ */
664
+ export interface CorsRule {
665
+ /**
666
+ * A unique identifier for this rule.
667
+ *
668
+ * @default - No id specified.
669
+ */
670
+ readonly id ?: string ;
671
+ /**
672
+ * The time in seconds that your browser is to cache the preflight response for the specified resource.
673
+ *
674
+ * @default - No caching.
675
+ */
676
+ readonly maxAge ?: number ;
677
+ /**
678
+ * Headers that are specified in the Access-Control-Request-Headers header.
679
+ *
680
+ * @default - No headers allowed.
681
+ */
682
+ readonly allowedHeaders ?: string [ ] ;
683
+ /**
684
+ * An HTTP method that you allow the origin to execute.
685
+ */
686
+ readonly allowedMethods : HttpMethods [ ] ;
687
+ /**
688
+ * One or more origins you want customers to be able to access the bucket from.
689
+ */
690
+ readonly allowedOrigins : string [ ] ;
691
+ /**
692
+ * One or more headers in the response that you want customers to be able to access from their applications.
693
+ *
694
+ * @default - No headers exposed.
695
+ */
696
+ readonly exposedHeaders ?: string [ ] ;
697
+ }
698
+
635
699
export interface BucketProps {
636
700
/**
637
701
* The kind of server-side encryption to apply to this bucket.
@@ -725,6 +789,15 @@ export interface BucketProps {
725
789
* @default - No metrics configuration.
726
790
*/
727
791
readonly metrics ?: BucketMetrics [ ] ;
792
+
793
+ /**
794
+ * The CORS configuration of this bucket.
795
+ *
796
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-cors.html
797
+ *
798
+ * @default - No CORS configuration.
799
+ */
800
+ readonly cors ?: CorsRule [ ] ;
728
801
}
729
802
730
803
/**
@@ -808,6 +881,7 @@ export class Bucket extends BucketBase {
808
881
private readonly versioned ?: boolean ;
809
882
private readonly notifications : BucketNotifications ;
810
883
private readonly metrics : BucketMetrics [ ] = [ ] ;
884
+ private readonly cors : CorsRule [ ] = [ ] ;
811
885
812
886
constructor ( scope : Construct , id : string , props : BucketProps = { } ) {
813
887
super ( scope , id , {
@@ -826,7 +900,8 @@ export class Bucket extends BucketBase {
826
900
lifecycleConfiguration : Lazy . anyValue ( { produce : ( ) => this . parseLifecycleConfiguration ( ) } ) ,
827
901
websiteConfiguration : this . renderWebsiteConfiguration ( props ) ,
828
902
publicAccessBlockConfiguration : props . blockPublicAccess ,
829
- metricsConfigurations : Lazy . anyValue ( { produce : ( ) => this . parseMetricConfiguration ( ) } )
903
+ metricsConfigurations : Lazy . anyValue ( { produce : ( ) => this . parseMetricConfiguration ( ) } ) ,
904
+ corsConfiguration : Lazy . anyValue ( { produce : ( ) => this . parseCorsConfiguration ( ) } )
830
905
} ) ;
831
906
832
907
applyRemovalPolicy ( resource , props . removalPolicy !== undefined ? props . removalPolicy : RemovalPolicy . Orphan ) ;
@@ -855,6 +930,8 @@ export class Bucket extends BucketBase {
855
930
856
931
// Add all bucket metric configurations rules
857
932
( props . metrics || [ ] ) . forEach ( this . addMetric . bind ( this ) ) ;
933
+ // Add all cors configuration rules
934
+ ( props . cors || [ ] ) . forEach ( this . addCorsRule . bind ( this ) ) ;
858
935
859
936
// Add all lifecycle rules
860
937
( props . lifecycleRules || [ ] ) . forEach ( this . addLifecycleRule . bind ( this ) ) ;
@@ -892,6 +969,15 @@ export class Bucket extends BucketBase {
892
969
this . metrics . push ( metric ) ;
893
970
}
894
971
972
+ /**
973
+ * Adds a cross-origin access configuration for objects in an Amazon S3 bucket
974
+ *
975
+ * @param rule The CORS configuration rule to add
976
+ */
977
+ public addCorsRule ( rule : CorsRule ) {
978
+ this . cors . push ( rule ) ;
979
+ }
980
+
895
981
/**
896
982
* Adds a bucket notification event destination.
897
983
* @param event The event to trigger the notification
@@ -1103,6 +1189,25 @@ export class Bucket extends BucketBase {
1103
1189
}
1104
1190
}
1105
1191
1192
+ private parseCorsConfiguration ( ) : CfnBucket . CorsConfigurationProperty | undefined {
1193
+ if ( ! this . cors || this . cors . length === 0 ) {
1194
+ return undefined ;
1195
+ }
1196
+
1197
+ return { corsRules : this . cors . map ( parseCors ) } ;
1198
+
1199
+ function parseCors ( rule : CorsRule ) : CfnBucket . CorsRuleProperty {
1200
+ return {
1201
+ id : rule . id ,
1202
+ maxAge : rule . maxAge ,
1203
+ allowedHeaders : rule . allowedHeaders ,
1204
+ allowedMethods : rule . allowedMethods ,
1205
+ allowedOrigins : rule . allowedOrigins ,
1206
+ exposedHeaders : rule . exposedHeaders
1207
+ } ;
1208
+ }
1209
+ }
1210
+
1106
1211
private parseTagFilters ( tagFilters ?: { [ tag : string ] : any } ) {
1107
1212
if ( ! tagFilters || tagFilters . length === 0 ) {
1108
1213
return undefined ;
0 commit comments