All Products
Search
Document Center

Object Storage Service:Configure CORS rules

Last Updated:Jul 31, 2024

Configures cross-origin resource sharing (CORS) rules for a bucket.

Command syntax

ossutil api put-bucket-cors --bucket value --cors-configuration value [flags]

Parameter

Type

Description

--bucket

string

The name of the bucket for which you want to configure CORS rules.

--cors-configuration

string

The container that stores the CORS rules of the bucket.

Note

The put-bucket-cors command corresponds to the PutBucketCors operation. For more information about the parameters in the command, see PutBucketCors.

--cors-configuration

The -- cors-configuration option supports both XML and JSON format:

  • XML format:

    <CORSConfiguration>
      <CORSRule>
        <AllowedOrigin>string</AllowedOrigin>
        ...
        <AllowedMethod>string</AllowedMethod>
        ...
        <AllowedHeader>string</AllowedHeader>
        <ExposeHeader>string</ExposeHeader>
        ...
        <MaxAgeSeconds>integer</MaxAgeSeconds>
      </CORSRule>
      ...
      <ResponseVary>boolean</ResponseVary>
    </CORSConfiguration>
    Note

    ... specifies that multiple nodes are supported.

  • JSON format:

    {
      "CORSRule": [
        {
          "AllowedOrigin": ["string", ...],
          "AllowedMethod": ["string", ...],
          "AllowedHeader": "string",
          "ExposeHeader": ["string", ...],
          "MaxAgeSeconds": integer
        },
        ...
      ],
      "ResponseVary": boolean
    }
    Note

    The root node of XML is removed in the JSON format.

Note

For more information, see Command-line options.

Examples

In the following examples, the allowed cross-origin request source is www.aliyun.com, the allowed cross-origin request methods are PUT and GET, and the cache response time is 10,000 seconds.

  • Use the XML configuration file named cors-configuration.xml:

    <CORSConfiguration>
      <CORSRule>
        <AllowedOrigin>www.aliyun.com</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>10000</MaxAgeSeconds>
      </CORSRule>
    </CORSConfiguration>
    ossutil api put-bucket-cors --bucket examplebucket --cors-configuration file://cors-configuration.xml 
  • Use the JSON configuration file named cors-configuration.json:

    {
      "CORSRule": {
        "AllowedOrigin": ["www.aliyun.com"],
        "AllowedMethod": ["PUT","GET"],
        "MaxAgeSeconds": 10000
      }
    }
    ossutil api put-bucket-cors --bucket examplebucket --cors-configuration file://cors-configuration.json
  • Use JSON configuration parameters:

    ossutil api put-bucket-cors --bucket examplebucket --cors-configuration  "{\"CORSRule\":{\"AllowedOrigin\":[\"www.aliyun.com\"],\"AllowedMethod\":[\"PUT\",\"GET\"],\"MaxAgeSeconds\":10000}}"