Skip to content

Commit dbca3a2

Browse files
committed
Tighten up S3 CORS configuration
1 parent dd334e2 commit dbca3a2

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed
81.1 KB
Loading
-667 KB
Loading

opensaas-sh/blog/src/content/docs/guides/file-uploading.mdx

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import defaultSettings from '@assets/file-uploads/default-settings.png';
1111
import newBucket from '@assets/file-uploads/new-bucket.png';
1212
import permissions from '@assets/file-uploads/permissions.png';
1313
import cors from '@assets/file-uploads/cors.png';
14+
import corsExample from '@assets/file-uploads/cors-example.png';
1415
import username from '@assets/file-uploads/username.png';
1516
import keys from '@assets/file-uploads/keys.png';
1617

@@ -61,52 +62,58 @@ To do so, follow the steps in this external guide: [Creating IAM users and S3 bu
6162
Once you are logged in with your IAM user, you'll need to create an S3 bucket to store your files.
6263

6364
1. Navigate to the S3 service in the AWS console
64-
<Image src={findS3} alt="find s3" loading="lazy" />
65+
<Image src={findS3} alt="find s3" loading="lazy" />
6566
2. Click on the `Create bucket` button
66-
<Image src={createBucket} alt="create bucket" loading="lazy" />
67+
<Image src={createBucket} alt="create bucket" loading="lazy" />
6768
3. Fill in the bucket name and region
6869
4. **Leave all the settings as default** and click `Create bucket`
69-
<Image src={defaultSettings} alt="bucket settings" loading="lazy" />
70+
<Image src={defaultSettings} alt="bucket settings" loading="lazy" />
7071

7172
### Change the CORS settings
7273

7374
Now we need to change some permissions on the bucket to allow for file uploads from your app.
7475

7576
1. Click on the bucket you just created
76-
<Image src={newBucket} alt="new bucket" loading="lazy" />
77+
<Image src={newBucket} alt="new bucket" loading="lazy" />
7778
2. Click on the `Permissions` tab
78-
<Image src={permissions} alt="permissions" loading="lazy" />
79+
<Image src={permissions} alt="permissions" loading="lazy" />
7980
3. Scroll down to the `Cross-origin resource sharing (CORS)` section and click `Edit`
80-
<Image src={cors} alt="cors" loading="lazy" />
81-
5. Paste the following CORS configuration and click `Save changes`:
82-
```json
83-
[
84-
{
85-
"AllowedHeaders": [
86-
"*"
87-
],
88-
"AllowedMethods": [
89-
"PUT",
90-
"GET"
91-
],
92-
"AllowedOrigins": [
93-
"*"
94-
],
95-
"ExposeHeaders": []
96-
}
97-
]
98-
```
81+
<Image src={cors} alt="cors" loading="lazy" />
82+
5. Insert the correct CORS configuration and click `Save changes`. You can
83+
copy-paste most of the config below, but **you must edit the
84+
`AllowedOrigins` field** to fit your app. Include `http://localhost:3000` for
85+
local development, and `https://<your domain>` for production.
86+
```json {11,12}
87+
[
88+
{
89+
"AllowedHeaders": [
90+
"*"
91+
],
92+
"AllowedMethods": [
93+
"PUT",
94+
"GET"
95+
],
96+
"AllowedOrigins": [
97+
"http://localhost:3000",
98+
"https://<your-domain>"
99+
],
100+
"ExposeHeaders": []
101+
}
102+
]
103+
```
104+
As an example, here are the CORS permissions for this site - https://opensaas.sh:
105+
<Image src={corsExample} alt="cors-example" loading="lazy" />
99106

100107
### Get your AWS S3 credentials
101108

102109
Now that you have your S3 bucket set up, you'll need to get your S3 credentials to use in your app.
103110

104111
1. Click on your username in the top right corner of the AWS console and select `Security Credentials`
105-
<Image src={username} alt="username" loading="lazy" />
112+
<Image src={username} alt="username" loading="lazy" />
106113
2. Scroll down to the `Access keys` section
107114
3. Click on `Create Access Key`
108115
4. Select the `Application running on an AWS service` option and create the access key
109-
<Image src={keys} alt="keys" loading="lazy" />
116+
<Image src={keys} alt="keys" loading="lazy" />
110117
5. Copy the `Access key ID` and `Secret access key` and paste them in your `src/app/.env.server` file:
111118
```sh
112119
AWS_S3_IAM_ACCESS_KEY=ACK...

0 commit comments

Comments
 (0)