CORS
A "blocked by CORS policy" error occurs when a website loads OSS resources because the browser's same-origin policy allows access only to resources with the same protocol, domain, and port. Configure Cross-Origin Resource Sharing (CORS) rules for OSS to securely authorize specified websites to access OSS resources.
How it works
The browser's same-origin policy allows a page to access only resources with the same protocol, domain, and port. A difference in any one of them makes the request cross-origin:
Current page | Requested resource | Cross-origin | Reason |
|
| No | Same origin |
|
| Yes | Different protocol |
|
| Yes | Different domain |
|
| Yes | Different port |
When a webpage requests OSS resources, the OSS domain differs from the webpage domain. The browser checks whether OSS explicitly allows the cross-origin access. If OSS has no matching CORS rule, the browser rejects the request and reports an error.
Configure CORS rules for OSS to tell the browser which cross-origin requests are allowed. If a rule matches, OSS returns headers such as Access-Control-Allow-Origin in the response. The browser allows the webpage to access the resource only after it receives these headers.
Configuration method
Go to the Bucket list and click the target bucket.
In the left-side navigation pane, choose , click Create Rule, and configure CORS rule parameters as needed.
Origin: Specify the websites that can access resources cross-origin, such as
https://www.example.com.Allowed Methods: Specify the allowed HTTP methods, such as
GET,PUT, andPOST.Allowed Headers: Specify the allowed request headers, such as
Authorizationandx-oss-*.Exposed Headers: Specify the response headers that frontend JavaScript can read, such as
ETag.Cache Timeout (Seconds): Specify how long to cache preflight results.
Vary: Origin: Enable this option for multiple origins or wildcard characters to prevent cache pollution.
Click OK to complete the configuration.
For detailed parameter descriptions, see Parameters of CORS rules. For specific settings, see the following scenario examples.
Scenario examples
The following scenarios show how to configure CORS for different business requirements.
Load static website resources
The website at https://www.example.com needs to load images, CSS, and JavaScript files from OSS.
Parameter | Value | Description |
Origin |
| Allow cross-origin requests only from this website. |
Allowed Methods |
|
|
Allowed Headers | Leave blank | This is a simple request, which does not trigger preflight. Therefore, this setting is not used. |
Exposed Headers |
|
|
Cache Timeout (Seconds) | 86400 | Cache preflight results for 24 hours to reduce preflight requests. |
Vary: Origin | Clear | The origin is single and fixed, so multi-domain caching is not a concern. |
Upload files directly from the frontend
Users upload avatars, documents, and other files to OSS from the webpage at https://app.example.com.
Parameter | Value | Description |
Origin |
| Make sure that only this authorized application can upload files. |
Allowed Methods |
|
|
Allowed Headers |
| Temporary signatures (STS or presigned URLs) secure direct frontend uploads. Use |
Exposed Headers |
|
|
Cache Timeout (Seconds) | 600 | Upload operations are relatively infrequent. Caching for 10 minutes reduces preflight requests while allowing configuration changes to take effect quickly. |
Vary: Origin | Select | Prepare for potential multi-domain deployment and prevent CDN cache pollution. |
Support for multiple environments
Multiple subdomains for development, testing, and production, such as dev.example.com and app.example.com, need to access the same OSS resources.
Parameter | Value | Description |
Origin |
| Use the |
Allowed Methods |
| Allow resource reads and uploads to meet multi-environment testing requirements. |
Allowed Headers |
| In multi-environment development, different environments can introduce different custom headers. Use |
Exposed Headers |
|
|
Cache Timeout (Seconds) | 3600 | A one-hour cache duration provides flexibility when switching and debugging multiple environments. |
Vary: Origin | Select | This option is required. It tells the CDN to distinguish cached content by the |
Make API-style calls with authentication
The frontend application at https://api.example.com needs to include custom headers such as Authorization to access protected OSS resources.
Parameter | Value | Description |
Origin |
| For requests that include authentication information, the origin must be a precise, trusted domain. |
Allowed Methods |
| Support full lifecycle management for reading, updating, and deleting private resources. |
Allowed Headers |
| Do not use |
Exposed Headers |
|
|
Cache Timeout (Seconds) | 600 | For authenticated requests, a shorter preflight cache duration helps security policy changes take effect faster. |
Vary: Origin | Select | Tell the CDN to cache requests with different origins separately to prevent conflicts. |
Types of CORS requests
Browsers classify cross-origin requests as simple requests or preflight requests based on their characteristics. The two types require slightly different CORS settings.
Type | Trigger conditions | Required CORS settings |
Simple request | All of the following conditions are met:
| Origin and allowed methods |
Preflight request | Any of the following conditions is met:
| Origin, allowed methods, and allowed headers |
Simple requests
Simple requests can be sent directly and are processed as follows:
The browser adds the
Originheader to the request. The header contains the resource origin, for example,Origin: https://www.example.com.OSS compares the HTTP method of the request and the value of the
Originheader against the target bucket's CORS configuration. If a matching item exists, OSS includes theAccess-Control-Allow-Originheader in the response.The browser receives the response and checks whether the
Access-Control-Allow-Originvalue matches the domain of the original request. If it matches, the request succeeds; otherwise, the request fails.
Preflight requests
A preflight request first asks whether the server allows the request and is processed as follows:
The browser sends an
OPTIONSrequest that includes the method (Access-Control-Request-Method) and headers (Access-Control-Request-Headers) of the main request.OSS uses the CORS configuration to check whether the request method and headers are allowed. If any method or header value in the preflight request is not in the target resource's set of allowed methods and headers, the request fails and the main request is not sent.
After the preflight succeeds, the same process as a simple request is performed.
Parameters of CORS rules
OSS checks rules from top to bottom in their configured order and uses the first rule that matches. After a rule matches, OSS does not check later rules.
Parameter | Required | Description |
Origin | Yes | Specify the websites or IP addresses that can access OSS resources cross-origin.
|
Allowed Methods | Yes | Specify the allowed HTTP methods.
|
Allowed Headers | No | Specify the HTTP headers allowed in actual requests. This setting applies to preflight requests.
|
Exposed Headers | No | Specify which headers in OSS responses frontend JavaScript can access.
To obtain |
Cache Timeout (Seconds) | No | Specify how long, in seconds, the browser caches an During the cache validity period, identical cross-origin requests for the same resource do not send another preflight request. |
Vary: Origin | No | Determine whether to add the This header tells CDNs and other intermediate caches to distinguish cached versions by the request Important You must enable this option when the origin has multiple domains or a wildcard character. Enabling it can reduce the CDN cache hit ratio. |
Apply in production
Security best practices
Configure precise origins: Unless the bucket is fully public, do not set the origin to
*. Specify website domains precisely.Restrict allowed methods: Expose only the HTTP methods required by your application. For read-only scenarios, configure only
GETandHEAD.Explicitly list allowed headers: Follow the principle of least privilege. Explicitly list the required request headers where possible, and avoid using
*.
Performance best practices
Optimize the preflight cache: Set a reasonable cache duration, such as 86400 seconds, to reduce preflight requests, latency, and request costs.
Evaluate the effect of Vary: Origin: Enabling this option prevents cache pollution, but can reduce the CDN cache hit ratio and increase back-to-origin traffic costs. Enable it only for multiple origins or wildcard characters.
Use with CDN
If a bucket has CDN acceleration enabled and you access it through an accelerated domain name, cross-origin requests first reach a CDN point of presence. You can configure CORS in either of the following ways:
Configure CORS in CDN: Configure CORS rules in the CDN console so that CDN directly returns CORS headers. For more information, see Configure cross-origin resource sharing.
Pass through CORS headers from the origin: CDN passes through the CORS headers returned by OSS. In this case, configure CORS rules in OSS.
CORS settings on OSS take effect only when requests directly access the OSS origin domain name or when CDN is configured to pass through origin response headers.
Limits
You can configure up to 20 CORS rules for each bucket.
FAQ
Why does the error “No 'Access-Control-Allow-Origin' header is present on the requested resource” occur?
This error usually occurs because the browser cached an old response without a CORS header, or the CORS rules are configured so that no rule matches the request. Troubleshoot the issue as follows:
Clear the browser cache and test again.
Check whether the CORS rule origin and allowed methods match the actual request.
Temporarily create a permissive CORS rule for troubleshooting: set the origin to
*, select all allowed methods, set allowed headers to*, set exposed headers toETagandx-oss-request-id, set the cache duration to0, and select Vary: Origin.If the issue persists, run the following command on a server to view the cross-origin request headers:
curl -v -H 'Origin: <origin-address>' '<OSS-resource-URL>' -o output.txtTroubleshoot according to the output:

If the response contains one cross-origin header that matches the configuration, the browser cache might be the cause. Press Ctrl+F5 to clear the browser cache, or set the cache duration to 0.
If the response contains two cross-origin headers or a header that does not match the configuration, OSS might be accelerated by CDN.
Log on to the CDN console and temporarily disable CDN acceleration to confirm that no cross-origin issue exists.
After confirmation, configure a custom response header in for the CDN-accelerated domain name.
If the issue remains unresolved, see Common errors and solutions for OSS Cross-Origin Resource Sharing (CORS) for further troubleshooting.
Why does the error “The 'Access-Control-Allow-Origin' header has a value '...' that is not equal to the supplied origin” occur?
The server returned the Access-Control-Allow-Origin header, but its value does not match the Origin of the current request. This is usually a cache issue: when multiple website domains access OSS, the browser or CDN might retain access permission for another website.
Solution: Enable Vary: Origin in the CORS rule, or clear the browser cache and access the resource again.
Why does the error “The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*' when credentials mode is 'include'” occur?
The frontend code sends a request with credentials (Access-Control-Allow-Credentials is True), but the origin is configured as the wildcard character *. For security reasons, browsers do not allow wildcard origins in this case.
Solution:
If
Credentialsmust remain in the request, change the origin from*to a specific domain, such ashttps://example.com, and make sure thatAccess-Control-Allow-Credentialsis set totrueon the server.If
Credentialsare not needed, setxhr.withCredentialstofalsein the frontend code, and make sure thatAccess-Control-Allow-Credentialsis set tofalseor is not returned by the server.
Why does object-level Access-Control-Allow-Origin not take effect?
Object-level Access-Control-Allow-Origin takes effect only when no bucket-level CORS rules are configured. If bucket-level CORS rules are configured, object-level Access-Control-Allow-Origin is ignored regardless of whether the request matches the rules.
How can you improve slow cross-origin loading from OSS?
The loading speed of cross-origin requests depends on the physical network path between the client and the OSS bucket, not on CORS itself. Long-distance access can occur when the client is in China (Hong Kong) and the bucket is in the Chinese mainland. Use OSS transfer acceleration to access the bucket.