Help Center/ Object Storage Service/ User Guide/ Data Security/ Configuring CORS to Allow Cross-Origin Access to OBS
Updated on 2026-09-23 GMT+08:00

Configuring CORS to Allow Cross-Origin Access to OBS

OBS provides cross-origin resource sharing (CORS) in HTML5 to enable cross-origin access.

You can create CORS rules or replicate existing CORS rules from another bucket.

Scenarios

In a typical web page request, the browser's same-origin policy (SOP) allows a page to access resources only when the protocol, domain name, and port are the same. Scripts and content from different origins (different protocols, domain names, or ports) cannot interact. In other words, due to the same-origin policy, JavaScript running in one domain cannot operate objects in another domain. OBS supports cross-origin resource sharing (CORS), allowing OBS resources to be accessed across origins.

CORS is a browser-standard mechanism defined by the World Wide Web Consortium (W3C). It defines a way for web client applications in one origin to interact with resources in another origin.

OBS supports CORS. OBS resources can be accessed across origins.

OBS supports static website hosting. Static websites stored in OBS can respond to website requests from another origin only when CORS is configured for the bucket where the website files are stored.

CORS:
  • Enables you to access OBS resources without using a proxy when using JavaScript and HTML5 to develop web applications.
  • Enables you to directly upload files to OBS with the dragging function of HTML5, view the progress, or update contents directly from web applications.
  • Enables external web pages, style sheets, or HTML5 applications hosted in different origins to share web fonts or images stored in OBS.

The CORS configuration takes effect within two minutes.

By default, OBS allows all domains to access the root domain through cross-origin requests, which may expose clients to security risks.

To avoid these risks, you can create a custom crossdomain.xml file in your bucket and add Security.loadPolicyFile("https://bucket.obs.ap-southeast-1.myhuaweicloud.com/crossdomain.xml") in your flash code. Replace bucket.obs.ap-southeast-1.myhuaweicloud.com with the actual domain name of your bucket.

How It Works

If two web pages use the same protocol, domain name (or IP address), and port, they are considered to be from the same origin. If any of these three elements is different, the web pages are considered to be from different origins. To better understand cross-origin rules, see Table 1.

Table 1 Same-origin check examples

Current Page

Requested Resource

Cross-Origin

Access Result

Cause

https://support.huaweicloud.com/dir/test.html

https://support.huaweicloud.com/dir/other.html

No

Succeeded

Same-origin (same protocol, domain name, and port)

https://support.huaweicloud.com/dir/test.html

https://support.huaweicloud.com/dir/inner/other.html

No

Succeeded

Same-origin (same protocol, domain name, and port)

https://support.huaweicloud.com/dir/test.html

http://support.huaweicloud.com/dir/test.html

Yes

Failed

Different protocols

https://support.huaweicloud.com/dir/test.html

https://support.huaweicloud.com:81/dir/test.html

Yes

Failed

Different ports

https://support.huaweicloud.com/dir/test.html

https://help.huaweicloud.com/dir/test.html

Yes

Failed

Different domain names

When a web page requests an OBS resource, the browser checks whether OBS explicitly allows cross-origin access because the web page and OBS use different domain names.

  • If no CORS rule is configured for OBS, the browser rejects the request and reports an error.
  • If CORS rules are configured for OBS, you can specify which cross-origin requests are allowed. When the CORS rules of the web page and OBS match, OBS returns headers such as Access-Control-Allow-Origin in the response. The browser allows the web page to access OBS resources only after receiving the response.

Constraints

A bucket can have a maximum of 100 CORS rules configured.

Important Notes

A CORS rule takes effect within two minutes after being configured.

Creating a CORS Rule

You can use OBS Console, APIs, or SDKs to create CORS rules. You cannot use OBS Browser+ or obsutil to do so.

Example Scenarios

The following describes how to configure CORS rules for different service scenarios.

CORS Request Types

CORS involves two request types: simple requests and preflight requests, distinguished by whether a preflight check is required.

  • Simple request: The actual request is sent directly without a preceding preflight request.
  • Preflight request: An initial OPTIONS request is sent to verify whether the server allows the actual request. After receiving a positive response, the actual request is sent.

    Preflight requests enhance the security of cross-origin access, prevent malicious websites from issuing harmful requests to OBS, and enable controlled cross-origin resource sharing.

Simple requests and preflight requests require different CORS rule configurations, as described in Table 7.

Table 7 CORS rule configurations for simple and preflight requests

Request Type

Trigger Condition

CORS Configuration

Description

Simple requests

All of the following conditions must be met:

  • Request method: GET/POST/HEAD
  • Request headers:
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type: application/x-www-form-urlencoded, multipart/form-data, and text/plain
  • Custom request headers: none

Allowed Origin

For simple requests, only the requested domain names are verified on the server. The request methods and request headers are not verified.

Preflight requests

Any of the following conditions are met:

  • Request method: any method (such as PUT/DELETE) except GET/POST/HEAD
  • Content-Type: any value (such as application/json) except application/x-www-form-urlencoded, multipart/form-data, and text/plain
  • Custom request headers: x-obs-* (as an example)

All three of the following parameters must be configured:

  • Allowed Origin
  • Allowed Method
  • Allowed Header

For preflight requests, the requested domain names, request methods, and request headers are verified on the server.

If these domain names, request methods, and request headers match those specified in the CORS rules configured on OBS, the client sends the actual request.

The following details the process of handling simple and preflight requests.

  1. The browser sends the actual request directly to the OBS server and automatically includes the Origin header (for example, Origin: https://www.example.com).
  2. The OBS server verifies the Origin header value against the CORS rules (the value of Allowed Origin).
    • If they match, OBS adds the Access-Control-Allow-Origin header (set to the value of Allowed Origin) to the response and returns it to the browser.
    • If they do not match, the request fails.
  3. After receiving the response, the browser checks whether the Access-Control-Allow-Origin header value matches the domain name in the original request. If they match, the request succeeds; otherwise, it fails.
  1. The browser sends an OPTIONS preflight request containing the domain name, request method, and request headers to the OBS server. This request does not include service data.
  2. The OBS server checks the domain name, request method, and request headers in the preflight request against the configured CORS rules (the values of Allowed Origin, Allowed Method, Allowed Header, and Cache Duration).
    • If the values match, the OBS server returns a response indicating that the preflight request is allowed.
    • If they do not fully match, the request fails and the actual request is not sent.
  3. After receiving the preflight response, the browser sends the actual request to the OBS server, following the same process as a simple request.

Replicating CORS Rules

You can use OBS Console to replicate CORS rules. You cannot use APIs, SDKs, OBS Browser+, or obsutil to do so.

  1. In the navigation pane of OBS Console, choose Buckets.
  2. In the bucket list, click the desired bucket. The Objects page is displayed.
  3. In the navigation pane, choose Data Security > CORS Rules.
  4. Click Replicate.
  5. Select a replication source, which is the bucket whose CORS rules you want to replicate.

    • The CORS rules replicated from a source bucket do not overwrite existing rules in the current bucket. Any rules that conflict with existing ones are not replicated.
    • The version of both the source and destination buckets must be 3.0.
    • There can be 100 CORS rules at most in a bucket. If the number of rules you plan to replicate plus the number of existing rules in the destination bucket exceeds 100, the replication will fail. Before replicating the rules, delete some if necessary.
    Figure 2 Replicating CORS rules

  6. Click OK to replicate the CORS rules to the current bucket.

Best Practices

  • Scenarios with high security requirements

    In scenarios with high security requirements, you are advised to configure CORS rules with attention to the settings of Allowed Origin, Allowed Method, and Allowed Header.

    • Accurately configuring the allowed origin: If the bucket is not fully open to the public, avoid using the wildcard (*) and specify the exact domain name.
    • Minimizing allowed methods: Evaluate the request methods required by the service and avoid setting too many allowed methods. For example, if you only need to view images in a bucket on a website, set the method to GET and HEAD.
    • Listing only necessary allowed headers: Follow the principle of least privilege and explicitly list the required request headers. Avoid using the wildcard (*).
  • Scenarios with high performance requirements

    In scenarios with high performance requirements, you are advised to reduce the number of preflight requests to improve access performance.

    You can set an appropriate cache duration (for example, 86,400 seconds) to reduce the number of preflight requests.

  • Working with CDN

    If CDN acceleration is enabled for a bucket and the CDN domain name is used to access it, cross-origin requests to OBS first reach the CDN PoP. There are two configuration modes:

    • Configure HTTP headers on CDN so that CDN directly returns the CORS headers. For details, see Configuring HTTP Headers.
    • Allow CDN to transparently pass through the CORS headers returned by OBS. In this case, configure CORS rules on OBS.

    The CORS rules configured on OBS take effect only when requests directly access the OBS origin server domain name or CDN transparently forwards the origin server's response headers.

References