Lesson 5 of 7 Amazon S3

Allowing Browser Access to Buckets with CORS

This lesson explains how cross-origin resource sharing (CORS), the browser mechanism that decides whether a web page on one origin may call resources on another, applies to Amazon S3. A bucket accessed from browser JavaScript on a different domain needs a CORS configuration.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what an origin is and why browsers block cross-origin responses without CORS headers.
  • Describe how S3 matches a request against the rules in a bucket's CORS configuration.
  • Explain why CORS does not grant access and identify the common causes of S3 CORS errors.

Why CORS is needed

An origin is the combination of scheme, host and port, such as https://app.example.com. Browsers block JavaScript from reading responses from another origin unless that origin's response includes Access-Control-Allow-Origin and related headers. The check is enforced only by browsers, so the same request succeeds from curl or a server.

For requests other than simple GETs, such as a PUT with a custom header, the browser first sends an OPTIONS preflight request, and S3 answers it from the bucket's CORS rules.

A CORS configuration

A bucket's CORS configuration is a list of rules. When a browser sends a preflight request or an actual cross-origin request, S3 looks for the first rule whose origin, method and headers match, and if one does, it returns the Access-Control-Allow-* headers that permit the request; if none matches, the browser never sends the actual request. A bucket accessed from a web page on another origin therefore needs a rule that allows the page's origin and methods, or the browser blocks the request even with a valid presigned URL. The following rule allows a web application to upload and download objects directly.

{
  "CORSRules": [{
    "AllowedOrigins": ["https://app.example.com"],
    "AllowedMethods": ["PUT", "GET"],
    "AllowedHeaders": ["*"],
    "ExposeHeaders": ["ETag"],
    "MaxAgeSeconds": 3000
  }]
}

Because CORS is enforced by the browser, not by S3, a request from a server or the CLI succeeds without any CORS rule, which is why an upload can work in a test script yet fail in the web application. The following table describes each element of the rule above.

ElementMeaning
AllowedOriginsOrigins allowed to make requests; use exact origins rather than * for private data
AllowedMethodsHTTP methods allowed: GET, PUT, POST, DELETE or HEAD
AllowedHeadersRequest headers the browser may send, checked during the preflight
ExposeHeadersResponse headers the browser may read, such as ETag for multipart uploads
MaxAgeSecondsHow long the browser may cache the preflight response

Troubleshooting CORS

CORS errors in S3 usually result from a small number of common causes. The following list describes the points to check.

  • CORS does not grant access. It only lets the browser read a response; the request still needs permission through a bucket policy or a presigned URL.
  • Origins must match exactly, including the scheme and any port: http://localhost:3000 and https://localhost:3000 are different origins.
  • Behind CloudFront, the distribution must forward the Origin header (and preflight headers) to S3, or S3 never sees the request's origin.
  • API Gateway has its own CORS settings; S3 CORS applies only to requests made to the bucket.
Worked example

Uploads that fail only in the browser

A single-page app at https://app.example.com uploads files to S3 with presigned PUT URLs. The same URLs work with curl, but the browser reports a CORS error.

CheckFinding
Bucket CORS configurationNone, so S3 rejects the browser's OPTIONS preflight
After adding a rule for GET onlyThe preflight for PUT still fails
After allowing PUT from the originUploads succeed, but multipart uploads fail because the browser cannot read ETag

The final rule allows PUT and GET from https://app.example.com, allows all request headers and exposes ETag. Permission to upload still comes from the presigned URL; CORS only lets the browser complete the request.

Check yourself

A request to S3 works from curl but fails with a CORS error in the browser. Why does curl succeed?

Show answer

CORS is enforced by browsers, not by S3 for other clients. The fix is a CORS configuration on the bucket that allows the page's origin, method and headers.

Exam Tips

Remember for the exam

  • A browser error mentioning Access-Control-Allow-Origin means CORS must be configured on the bucket.
  • A request that works from curl but fails in the browser points to CORS.
  • CORS rules do not authorise access; permissions still come from policies or presigned URLs.
  • Expose the ETag header for browser multipart uploads.