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

Configuring CORS to Allow Cross-Origin Access to OBS

This section describes how to use CORS in HTML5 to enable cross-origin access.

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

Context

Cross-origin access refers to access between different domains.

For browser security purposes, cross-origin access is typically restricted by the same-origin policy. Under this policy, resources in a domain cannot be accessed by a different domain using JavaScript.

If two web pages have the same protocol, domain name or IP, and port (if specified), they have the same origin. Table 1 shows you how to determine whether the URLs listed in the table have the same origin as https://support.huaweicloud.com/dir/test.html.

Table 1 Cross-origin analysis

URL

Access Result

Reason

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

Succeeded

Same protocol, domain name, and port

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

Succeeded

Same protocol, domain name, and port

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

Failed

Same domain name and port, but different protocols

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

Failed

Same protocol and domain name, but different ports

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

Failed

Same protocol and port, but different domain names

Scenarios

CORS (cross-origin resource sharing) is a browser-standard mechanism provided by the World Wide Web Consortium (W3C). It defines how a web client in one origin interacts with resources in another origin. For general web page requests, website scripts and contents in one origin cannot interact with those in another origin because of Same Origin Policies (SOPs).

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 about two minutes to take effect.

OBS is configured to support cross-origin access using the root domain name by default. To prevent potential attacks:

You can create a crossdomain.xml file with specific rules in the bucket for your client and add then Security.loadPolicyFile("https://bucket.obs.ap-southeast-1.myhuaweicloud.com/crossdomain.xml") in the flash code of the file. bucket.obs.ap-southeast-1.myhuaweicloud.com needs to be replaced with the actual access domain name of your bucket.

Prerequisites

Static website hosting has been configured. For details, see Static Website Hosting.

Creating a CORS Rule

You can use OBS Console, APIs, or SDKs to create CORS rules.

Replicating CORS Rules

You can only use OBS Console to replicate CORS rules.

  1. In the navigation pane of OBS Console, choose Object Storage.
  2. In the bucket list, click the bucket you want to operate. The Objects page is displayed.
  3. In the navigation pane, choose Overview.
  4. In the Basic Configurations area, click CORS Rules. The CORS Rules page is displayed.

    Alternatively, you can choose Permissions > CORS Rules in the navigation pane.

  5. Click Replicate.
  6. Select a replication source, which is the bucket whose CORS rules you want to replicate.

    • The CORS rules replicated from a source bucket will not overwrite existing rules in the destination bucket, and any that conflict with the existing ones will not be replicated.
    • Both source and destination buckets must be of the 3.0 version.
    • You can remove the rules that you do not want to replicate.
    • There can be 100 CORS rules at most in a bucket. If the number of rules you will 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

  7. Click OK to replicate the CORS rules to the destination bucket.