Skip to content

Commit cfc3bc4

Browse files
authored
Enable CORS on S3 Bucket (#1026)
1 parent 109cd1b commit cfc3bc4

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

.changeset/loud-sheep-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-storage': patch
3+
---
4+
5+
Enable CORS on the S3 Bucket

.eslint_dictionary.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"cognito",
2222
"commonjs",
2323
"corepack",
24+
"cors",
2425
"ctor",
2526
"darwin",
2627
"datastore",

packages/backend-storage/src/construct.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ void describe('AmplifyStorage', () => {
4242
);
4343
});
4444

45+
void it('enables cors on the bucket', () => {
46+
const app = new App();
47+
const stack = new Stack(app);
48+
new AmplifyStorage(stack, 'testAuth', { name: 'testName' });
49+
50+
const template = Template.fromStack(stack);
51+
template.hasResourceProperties('AWS::S3::Bucket', {
52+
CorsConfiguration: {
53+
CorsRules: [
54+
{
55+
AllowedHeaders: ['*'],
56+
AllowedMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE'],
57+
AllowedOrigins: ['*'],
58+
ExposedHeaders: [
59+
'x-amz-server-side-encryption',
60+
'x-amz-request-id',
61+
'x-amz-id-2',
62+
'ETag',
63+
],
64+
MaxAge: 3000,
65+
},
66+
],
67+
},
68+
});
69+
});
70+
4571
void describe('storeOutput', () => {
4672
void it('stores output using the provided strategy', () => {
4773
const app = new App();

packages/backend-storage/src/construct.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Construct } from 'constructs';
2-
import { Bucket, BucketProps, EventType, IBucket } from 'aws-cdk-lib/aws-s3';
2+
import {
3+
Bucket,
4+
BucketProps,
5+
EventType,
6+
HttpMethods,
7+
IBucket,
8+
} from 'aws-cdk-lib/aws-s3';
39
import {
410
BackendOutputStorageStrategy,
511
ConstructFactory,
@@ -57,6 +63,26 @@ export class AmplifyStorage
5763

5864
const bucketProps: BucketProps = {
5965
versioned: props.versioned || false,
66+
cors: [
67+
{
68+
maxAge: 3000,
69+
exposedHeaders: [
70+
'x-amz-server-side-encryption',
71+
'x-amz-request-id',
72+
'x-amz-id-2',
73+
'ETag',
74+
],
75+
allowedHeaders: ['*'],
76+
allowedOrigins: ['*'],
77+
allowedMethods: [
78+
HttpMethods.GET,
79+
HttpMethods.HEAD,
80+
HttpMethods.PUT,
81+
HttpMethods.POST,
82+
HttpMethods.DELETE,
83+
],
84+
},
85+
],
6086
};
6187
this.resources = {
6288
bucket: new Bucket(this, 'Bucket', bucketProps),

0 commit comments

Comments
 (0)