Skip to content

Cross-Origin Resource Sharing (CORS) in Object Storage

Last updated on

Configure Cross-Origin Resource Sharing (CORS) for a bucket to make its objects accessible to web applications hosted on a different domain. CORS is a browser security mechanism that controls access to resources from other origins.

For example, if you have images in a bucket named my-example-bucket and want to display them on a website at https://example.com, you add a CORS rule to my-example-bucket that allows access from https://example.com.

A CORS rule can be defined as XML or JSON. Each rule consists of:

Create a CORS configuration file in JSON format. The following example allows GET and HEAD access from my-app.com:

{
"CORSRules": [
{
"ID": "Allow Bucket for my-app.com",
"AllowedOrigins": [
"https://www.my-app.com",
"http://www.my-app.com",
"https://my-app.com",
"http://my-app.com"
],
"AllowedMethods": ["GET", "HEAD"],
"AllowedHeaders": ["Content-*", "Host"],
"ExposeHeaders": ["ETag"],
"MaxAgeSeconds": 86400
}
]
}

Apply the configuration file to your bucket:

Terminal window
aws s3api put-bucket-cors \
--bucket my-example-bucket \
--cors-configuration file://cors-rules.json \
--endpoint-url https://object.storage.eu01.onstackit.cloud

Retrieve the current CORS configuration for a bucket:

Terminal window
aws s3api get-bucket-cors \
--bucket my-example-bucket \
--endpoint-url https://object.storage.eu01.onstackit.cloud

To remove all CORS rules from a bucket:

Terminal window
aws s3api delete-bucket-cors \
--bucket my-example-bucket \
--endpoint-url https://object.storage.eu01.onstackit.cloud