Updated on 2024-04-01 GMT+08:00

Configuring CORS

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

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

Prerequisites

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

Creating a CORS Rule

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

    Figure 1 Overview > Basic Configurations > CORS Rules

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

    Figure 2 Permissions > CORS Rules

  5. Click Create. The Create CORS Rule dialog box is displayed. See Figure 3 for details.

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

    Figure 3 Creating a CORS rule

  6. In the CORS Rule dialog box, configure Allowed Origin, Allowed Method, Allowed Header, Exposed Header, and Cache Duration (s).

    If CDN acceleration is enabled for the bucket, HTTP headers must be configured on CDN. For details, see HTTP Header Settings.

    Table 1 Parameters in CORS rules

    Parameter

    Description

    Allowed Origin

    Mandatory

    Specifies the origins from which requests can access the bucket.

    Multiple matching rules are allowed. One rule occupies one line, and allows one wildcard character (*) at most. An example is given as follows:

    http://rds.example.com
    https://*.vbs.example.com

    Allowed Method

    Mandatory

    Specifies the allowed request methods for buckets and objects.

    The methods include Get, Post, Put, Delete, and Head.

    Allowed Header

    Optional

    Specifies the allowed headers in cross-origin requests.

    Only CORS requests matching the allowed headers are valid.

    You can enter multiple allowed headers (one per line) and each line can contain one wildcard character (*) at most. Spaces and special characters including &:< are not allowed.

    Exposed Header

    Optional

    Specifies the exposed headers in CORS responses, providing additional information for clients.

    By default, a browser can access only headers Content-Length and Content-Type. If the browser wants to access other headers, you need to configure those headers in this parameter.

    You can enter multiple exposed headers (one per line). Spaces and special characters including *&:< are not allowed.

    Cache Duration (s)

    Mandatory

    Specifies the duration that your browser can cache CORS responses, expressed in seconds. The default value is 100.

  7. Click OK.

    Message "The CORS rule created successfully." is displayed. The CORS configuration takes effect within two minutes.

    After CORS is successfully configured, only the addresses specified for Allowed Origin can access the bucket using the methods specified for Allowed Method. For example, you can configure CORS parameters for bucket testbucket as follows:

    • Allowed Origin: https://www.example.com
    • Allowed Method: GET
    • Allowed Header: *
    • Exposed Header: *
    • Cache Duration (s): 100

    By doing so, OBS only allows GET requests from https://www.example.com to access bucket testbucket, without restrictions on request headers. The client can cache CORS responses for 100 seconds.

Replicating 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 to go to the Objects page.
  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 4 Replicating CORS rules

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