Updated on 2026-07-27 GMT+08:00

Configuring CORS for a Bucket

Function

Cross-origin resource sharing (CORS) is a standard mechanism proposed by World Wide Web Consortium (W3C) and allows cross-origin requests from clients. You can call this API to configure CORS for a bucket so that websites hosted on OBS can respond to cross-origin requests from other websites. For more information about CORS, see Configuring CORS to Allow Cross-Origin Access to OBS.

Authorization

To call this API, you must be the bucket owner or have the permission to configure bucket CORS. You are advised to use IAM or bucket policies for authorization. For details about OBS authorization methods, see Differences Between OBS Permissions Control Methods.

  • If you use IAM for authorization, you need to use either role/policy-based authorization or identity policy-based authorization and configure the required permissions:
    • If you use role/policy-based authorization (IAM v3 APIs in the old IAM version), you must have the obs:bucket:PutBucketCORS permission. For details, see Creating a Custom IAM Policy.
    • If you use identity policy-based authorization (IAM v5 APIs in the new IAM version), you must have the obs:bucket:putBucketCORS permission, as shown in the following table. For details, see Creating a Custom IAM Identity Policy.

      Action

      Access Level

      Resource Type (*: Required)

      Condition Key

      Alias

      Dependencies

      obs:bucket:putBucketCORS

      Write

      bucket *

      -

      -

      -

      • obs:EpochTime
      • obs:SourceIp
      • obs:TlsVersion
      • obs:CustomDomain
  • If you use bucket policies for authorization, you must have the obs:bucket:PutBucketCORS permission. For details, see Creating a Custom Bucket Policy.

URI

PUT /

Calling Method

For details, see Calling APIs. Before calling this API, calculate the API signature and add it to the request.

You can debug this API in API Explorer.

Request Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
PUT /?cors HTTP/1.1 
Host: bucketname.obs.region.myhuaweicloud.com 
Content-Length: length
Date: date
Content-Type: application/xml
Authorization: authorization
Content-SHA256: SHA256
Content-MD5: MD5
<?xml version="1.0" encoding="UTF-8"?> 
<CORSConfiguration> 
    <CORSRule> 
        <ID>id</ID> 
        <AllowedMethod>method</AllowedMethod> 
        <AllowedOrigin>origin</AllowedOrigin> 
        <AllowedHeader>header</AllowedHeader> 
        <MaxAgeSeconds>seconds</MaxAgeSeconds> 
        <ExposeHeader>header</ExposeHeader>
    </CORSRule> 
</CORSConfiguration>

URI Parameters

In this request, the ?cors parameter in the URI identifies the request as related to CORS. No other URI parameters are required.

Request Headers

This request uses common headers and CORS request headers. For details, see Table 3 and Table 1.

Table 1 CORS request headers

Header

Type

Mandatory

Description

Content-SHA256

String

Yes

Definition

SHA-256 digest string of the message body, that is, the string obtained after the 256-bit SHA-256 value of the message body is encoded using Base64

Example: ogX9qClMrVJUBiUSIKDFM0qO41jJM0I5SCN55/OtMyI=

Default Value

N/A

Content-MD5

String

Yes

Definition

Base64-encoded 128-bit MD5 digest of the message according to RFC 1864 You can also configure the Content-SHA256 header whose value is the Base64-encoded SHA-256 digest of the message. Configure either Content-MD5 or Content-SHA256.

Example: n58IG6hfM7vqI4K0vnWpog==

Constraints

N/A

Range

N/A

Default Value

N/A

Request Body

In this request body, you must configure the CORS rules for a bucket in XML format. Table 2 describes the specific configuration elements.

Table 2 CORS configuration elements

Element

Type

Mandatory

Description

CORSConfiguration

Container

Yes

Definition

Root node of CORSRules.

Parent: N/A

Constraints

The maximum size is 64 KB.

Range

N/A

Default Value

N/A

CORSRule

Container

Yes

Definition

A CORS rule.

Parent: CORSConfiguration

Constraints

CORSConfiguration can contain a maximum of 100 rules.

Range

N/A

Default Value

N/A

ID

String

No

Definition

ID of a CORS rule.

Parent: CORSRule

Constraints

The ID cannot be longer than 255 characters.

Range

The value can contain 1 to 255 characters.

Default Value

N/A

AllowedMethod

String

Yes

Definition

Allowed HTTP methods (types of operations on buckets and objects) for a cross-origin request.

Parent: CORSRule

Constraints

N/A

Range

The following HTTP methods are supported:

  • GET
  • PUT
  • HEAD
  • POST
  • DELETE

Default Value

N/A

AllowedOrigin

String

Yes

Definition

Origin that is allowed to access the bucket.

Parent: CORSRule

Constraints

Only domain names consisting of ASCII characters are supported. Regular expressions are used for matching. Each rule allows at most one asterisk (*). A complete protocol (such as http or https) must be included.

For example, https://*.vbs.example.com.

Range

The value must comply with the CORS protocol and contain 0 to 20480 characters.

Default Value

N/A

AllowedHeader

String

No

Definition

What headers are allowed in a CORS request. If a CORS request contains the Access-Control-Request-Headers header, the headers listed in this header must match the headers in the AllowedHeader element so that the request can be considered valid. The matching is based on regular expressions.

Parent: CORSRule

Constraints

At most one asterisk (*) is allowed. Spaces, ampersands (&), colons (:), less-than signs (<), and full-width characters are not allowed.

Range

The value must comply with the CORS protocol and contain 0 to 20480 characters.

Default Value

N/A

MaxAgeSeconds

Integer

No

Definition

How long the response can be cached on a client.

Parent: CORSRule

Constraints

Each CORS rule can contain at most one MaxAgeSeconds.

Range

An integer greater than or equal to 0, in seconds

Default Value

3000

ExposeHeader

String

No

Definition

Specifies additional headers allowed in the response by a CORS rule, which are used to provide extra information to clients. By default, a browser can access only headers Content-Length and Content-Type. If the browser needs to access other headers, you need to configure them in this parameter.

Parent: CORSRule

Constraints

Spaces, asterisks (*), ampersands (&), colons (:), less-than signs (<), and full-width characters are not allowed.

Range

The value that complies with the CORS

Default Value

N/A

Response Syntax

1
2
3
4
HTTP/1.1 status_code

Date: date
Content-Length: length

Response Headers

This response uses common headers. For details, see Table 1.

Response Body

This response does not contain a response body.

Error Responses

No special errors. You can find all errors in Error Code Overview.

Sample Request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PUT /?cors HTTP/1.1
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Accept: */*
Date: Wed, 01 Jul 2015 03:51:52 GMT
Content-Type: application/xml
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:lq7BGoqE9yyhdEwE6KojJ7ysVxU=
Content-SHA256: ogX9qClMrVJUBiUSIKDFM0qO41jJM0I5SCN55/OtMyI=
Content-MD5: NGLzvw81f/A2C9PiGO0aZQ==
Content-Length: 617

<?xml version="1.0" encoding="utf-8"?>
<CORSConfiguration> 
  <CORSRule> 
    <AllowedMethod>POST</AllowedMethod>  
    <AllowedMethod>GET</AllowedMethod>  
    <AllowedMethod>HEAD</AllowedMethod>  
    <AllowedMethod>PUT</AllowedMethod>  
    <AllowedMethod>DELETE</AllowedMethod>  
    <AllowedOrigin>http://www.example.com</AllowedOrigin>  
    <AllowedHeader>AllowedHeader_1</AllowedHeader>  
    <AllowedHeader>AllowedHeader_2</AllowedHeader>  
    <MaxAgeSeconds>100</MaxAgeSeconds>  
    <ExposeHeader>ExposeHeader_1</ExposeHeader>  
    <ExposeHeader>ExposeHeader_2</ExposeHeader>
  </CORSRule>
</CORSConfiguration>

Sample Response

1
2
3
4
5
6
7
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Server: OBS
x-obs-request-id: BF26000001643627112BD03512FC94A4
x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCSYi6wLC4bkrvuS9sqnlRjxK2a5Fe3ry
Date: Wed, 01 Jul 2015 03:51:52 GMT
Content-Length: 0

Sample Request: Configuring Two CORS Rules for a Bucket

PUT /?cors HTTP/1.1
Authorization: OBS H4IPJX0TQTHTHEBQQCEC:iqSPeUBl66PwXDApxjRKk6hlcN4=
User-Agent: curl/7.29.0
Host: examplebucket.obs.region.myhuaweicloud.com
Date: Wed, 01 Jul 2015 02:37:22 GMT
Content-Type: application/xml
Content-MD5: HwVUAzslyD0rroMp/eIdwQ==
 
<CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>http://www.example.com</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
      
    </CORSRule>
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
 
    </CORSRule>
</CORSConfiguration>

Sample Response: Configuring Two CORS Rules for a Bucket

HTTP/1.1 100 Continue
HTTP/1.1 200 OK
x-obs-id-2: 32AAAQAAEAABSAAgAAEAABAAAQAAEAABCTPXg+yj9IXC9r6mgmWgfSfqQGvHM3rS
x-obs-request-id: 0000018A3A14051AD2886D166EE13D98
Server: OBS
Content-Length: 0
Date: Wed, 01 Jul 2015 02:37:22 GMT

Using SDKs to Call APIs

You are advised to use OBS SDKs to call APIs. SDKs encapsulate APIs to simplify development. You can call SDK API functions to access OBS without manually calculating signatures.

Java

Python

C

Go

BrowserJS: not supported

.NET

Android

iOS

PHP

Node.js