Cross-Origin Resource Sharing (CORS) allows web applications hosted on one domain to access resources from a different domain securely. This guide explains how to configure CORS for buckets in our Ceph Object Storage using s3cmd.
Step 1: Prepare a CORS Configuration File
Create an XML file named cors.xml with content similar to:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>https://example.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>ETag</ExposeHeader>
<ExposeHeader>Accept-Ranges</ExposeHeader>
<ExposeHeader>Content-Encoding</ExposeHeader>
<ExposeHeader>Content-Range</ExposeHeader>
</CORSRule>
</CORSConfiguration>
Replace https://example.com with the origin domain allowed to access the bucket. You can define multiple domains by adding another <AllowedOrigin>https://example.com</AllowedOrigin> configuration line within the XML file.
Step 2: Apply the CORS Policy to the Bucket
Run the following command to set the CORS policy on your bucket:
s3cmd setcors cors.xml s3://nameofyourbucket
Step 3: Verify the CORS Configuration
You can verify if CORS is correctly applied by running a CORS preflight request:
curl -i -X OPTIONS \ -H "Origin: https://example.com" \ -H "Access-Control-Request-Method: GET" \ https://objectstore.surf.nl/nameofyourbucket/your-object.jpg
Make sure your bucket policy or ACL allows the required actions (
s3:GetObject).