forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s3): add CORS Property to S3 Bucket (aws#2101)
Add CORS Property to S3 Bucket for configuring bucket cross-origin access rules. You can either specify the metrics as properties: new Bucket(stack, 'Bucket', { cors: [ { allowedHeaders: [ "*" ], allowedMethods: [ "GET" ], allowedOrigins: [ "*" ], exposedHeaders: [ "Date" ], id: "myCORSRuleId1", maxAge: 3600 } ] }); Or use the `addCors` function: const bucket = new Bucket(stack, 'Bucket'); bucket.addCors({ allowedMethods: ["GET", "HEAD"], allowedOrigins: ["https://example.com"] });
- Loading branch information
Showing
2 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { expect, haveResource } from '@aws-cdk/assert'; | ||
import { Stack } from '@aws-cdk/cdk'; | ||
import { Test } from 'nodeunit'; | ||
import { Bucket } from '../lib'; | ||
|
||
export = { | ||
'Can use addCors() to add a CORS configuration'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
// WHEN | ||
const bucket = new Bucket(stack, 'Bucket'); | ||
bucket.addCors({ | ||
allowedMethods: ["GET", "HEAD"], | ||
allowedOrigins: ["https://example.com"] | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::S3::Bucket', { | ||
CorsConfiguration: { | ||
CorsRules: [{ | ||
AllowedMethods: ["GET", "HEAD"], | ||
AllowedOrigins: ["https://example.com"] | ||
}] | ||
} | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
'Bucket with multiple cors configurations'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
// WHEN | ||
new Bucket(stack, 'Bucket', { | ||
cors: [ | ||
{ | ||
allowedHeaders: [ | ||
"*" | ||
], | ||
allowedMethods: [ | ||
"GET" | ||
], | ||
allowedOrigins: [ | ||
"*" | ||
], | ||
exposedHeaders: [ | ||
"Date" | ||
], | ||
id: "myCORSRuleId1", | ||
maxAge: 3600 | ||
}, | ||
{ | ||
allowedHeaders: [ | ||
"x-amz-*" | ||
], | ||
allowedMethods: [ | ||
"DELETE" | ||
], | ||
allowedOrigins: [ | ||
"http://www.example1.com", | ||
"http://www.example2.com" | ||
], | ||
exposedHeaders: [ | ||
"Connection", | ||
"Server", | ||
"Date" | ||
], | ||
id: "myCORSRuleId2", | ||
maxAge: 1800 | ||
} | ||
] | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::S3::Bucket', { | ||
CorsConfiguration: { | ||
CorsRules: [ | ||
{ | ||
AllowedHeaders: [ | ||
"*" | ||
], | ||
AllowedMethods: [ | ||
"GET" | ||
], | ||
AllowedOrigins: [ | ||
"*" | ||
], | ||
ExposedHeaders: [ | ||
"Date" | ||
], | ||
Id: "myCORSRuleId1", | ||
MaxAge: 3600 | ||
}, | ||
{ | ||
AllowedHeaders: [ | ||
"x-amz-*" | ||
], | ||
AllowedMethods: [ | ||
"DELETE" | ||
], | ||
AllowedOrigins: [ | ||
"http://www.example1.com", | ||
"http://www.example2.com" | ||
], | ||
ExposedHeaders: [ | ||
"Connection", | ||
"Server", | ||
"Date" | ||
], | ||
Id: "myCORSRuleId2", | ||
MaxAge: 1800 | ||
} | ||
] | ||
} | ||
})); | ||
|
||
test.done(); | ||
}, | ||
}; |