@@ -790,12 +790,19 @@ export interface BucketProps {
790
790
/**
791
791
* Specifies the redirect behavior of all requests to a website endpoint of a bucket.
792
792
*
793
- * If you specify this property, you can't specify "websiteIndexDocument" nor "websiteErrorDocument ".
793
+ * If you specify this property, you can't specify "websiteIndexDocument", "websiteErrorDocument" nor , "websiteRoutingRules ".
794
794
*
795
795
* @default - No redirection.
796
796
*/
797
797
readonly websiteRedirect ?: RedirectTarget ;
798
798
799
+ /**
800
+ * Rules that define when a redirect is applied and the redirect behavior
801
+ *
802
+ * @default - No redirection rules.
803
+ */
804
+ readonly websiteRoutingRules ?: RoutingRule [ ] ;
805
+
799
806
/**
800
807
* Specifies a canned ACL that grants predefined permissions to the bucket.
801
808
*
@@ -1256,22 +1263,40 @@ export class Bucket extends BucketBase {
1256
1263
}
1257
1264
1258
1265
private renderWebsiteConfiguration ( props : BucketProps ) : CfnBucket . WebsiteConfigurationProperty | undefined {
1259
- if ( ! props . websiteErrorDocument && ! props . websiteIndexDocument && ! props . websiteRedirect ) {
1266
+ if ( ! props . websiteErrorDocument && ! props . websiteIndexDocument && ! props . websiteRedirect && ! props . websiteRoutingRules ) {
1260
1267
return undefined ;
1261
1268
}
1262
1269
1263
1270
if ( props . websiteErrorDocument && ! props . websiteIndexDocument ) {
1264
1271
throw new Error ( `"websiteIndexDocument" is required if "websiteErrorDocument" is set` ) ;
1265
1272
}
1266
1273
1267
- if ( props . websiteRedirect && ( props . websiteErrorDocument || props . websiteIndexDocument ) ) {
1268
- throw new Error ( '"websiteIndexDocument" and "websiteErrorDocument " cannot be set if "websiteRedirect" is used' ) ;
1274
+ if ( props . websiteRedirect && ( props . websiteErrorDocument || props . websiteIndexDocument || props . websiteRoutingRules ) ) {
1275
+ throw new Error ( '"websiteIndexDocument", "websiteErrorDocument" and, "websiteRoutingRules " cannot be set if "websiteRedirect" is used' ) ;
1269
1276
}
1270
1277
1278
+ const routingRules = props . websiteRoutingRules ? props . websiteRoutingRules . map < CfnBucket . RoutingRuleProperty > ( ( rule ) => {
1279
+ if ( rule . condition && ! rule . condition . httpErrorCodeReturnedEquals && ! rule . condition . keyPrefixEquals ) {
1280
+ throw new Error ( 'The condition property cannot be an empty object' ) ;
1281
+ }
1282
+
1283
+ return {
1284
+ redirectRule : {
1285
+ hostName : rule . hostName ,
1286
+ httpRedirectCode : rule . httpRedirectCode ,
1287
+ protocol : rule . protocol ,
1288
+ replaceKeyWith : rule . replaceKey && rule . replaceKey . withKey ,
1289
+ replaceKeyPrefixWith : rule . replaceKey && rule . replaceKey . prefixWithKey ,
1290
+ } ,
1291
+ routingRuleCondition : rule . condition
1292
+ } ;
1293
+ } ) : undefined ;
1294
+
1271
1295
return {
1272
1296
indexDocument : props . websiteIndexDocument ,
1273
1297
errorDocument : props . websiteErrorDocument ,
1274
1298
redirectAllRequestsTo : props . websiteRedirect ,
1299
+ routingRules
1275
1300
} ;
1276
1301
}
1277
1302
}
@@ -1485,6 +1510,89 @@ export enum BucketAccessControl {
1485
1510
AWS_EXEC_READ = 'AwsExecRead' ,
1486
1511
}
1487
1512
1513
+ export interface RoutingRuleCondition {
1514
+ /**
1515
+ * The HTTP error code when the redirect is applied
1516
+ *
1517
+ * In the event of an error, if the error code equals this value, then the specified redirect is applied.
1518
+ *
1519
+ * If both condition properties are specified, both must be true for the redirect to be applied.
1520
+ *
1521
+ * @default - The HTTP error code will not be verified
1522
+ */
1523
+ readonly httpErrorCodeReturnedEquals ?: string ;
1524
+
1525
+ /**
1526
+ * The object key name prefix when the redirect is applied
1527
+ *
1528
+ * If both condition properties are specified, both must be true for the redirect to be applied.
1529
+ *
1530
+ * @default - The object key name will not be verified
1531
+ */
1532
+ readonly keyPrefixEquals ?: string ;
1533
+ }
1534
+
1535
+ export class ReplaceKey {
1536
+ /**
1537
+ * The specific object key to use in the redirect request
1538
+ */
1539
+ public static with ( keyReplacement : string ) {
1540
+ return new this ( keyReplacement ) ;
1541
+ }
1542
+
1543
+ /**
1544
+ * The object key prefix to use in the redirect request
1545
+ */
1546
+ public static prefixWith ( keyReplacement : string ) {
1547
+ return new this ( undefined , keyReplacement ) ;
1548
+ }
1549
+
1550
+ private constructor ( public readonly withKey ?: string , public readonly prefixWithKey ?: string ) {
1551
+ }
1552
+ }
1553
+
1554
+ /**
1555
+ * Rule that define when a redirect is applied and the redirect behavior.
1556
+ *
1557
+ * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
1558
+ */
1559
+ export interface RoutingRule {
1560
+ /**
1561
+ * The host name to use in the redirect request
1562
+ *
1563
+ * @default - The host name used in the original request.
1564
+ */
1565
+ readonly hostName ?: string ;
1566
+
1567
+ /**
1568
+ * The HTTP redirect code to use on the response
1569
+ *
1570
+ * @default "301" - Moved Permanently
1571
+ */
1572
+ readonly httpRedirectCode ?: string ;
1573
+
1574
+ /**
1575
+ * Protocol to use when redirecting requests
1576
+ *
1577
+ * @default - The protocol used in the original request.
1578
+ */
1579
+ readonly protocol ?: RedirectProtocol ;
1580
+
1581
+ /**
1582
+ * Specifies the object key prefix to use in the redirect request
1583
+ *
1584
+ * @default - The key will not be replaced
1585
+ */
1586
+ readonly replaceKey ?: ReplaceKey ;
1587
+
1588
+ /**
1589
+ * Specifies a condition that must be met for the specified redirect to apply.
1590
+ *
1591
+ * @default - No condition
1592
+ */
1593
+ readonly condition ?: RoutingRuleCondition ;
1594
+ }
1595
+
1488
1596
function mapOrUndefined < T , U > ( list : T [ ] | undefined , callback : ( element : T ) => U ) : U [ ] | undefined {
1489
1597
if ( ! list || list . length === 0 ) {
1490
1598
return undefined ;
0 commit comments