文档

设置跨域资源共享规则

更新时间:

您可以通过API级命令设置跨域资源共享规则。

命令格式

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

参数

类型

说明

--bucket

string

Bucket名称。

--cors-configuration

string

Bucket的CORS规则容器。

说明

put-bucket-cors命令对应API接口PutBucketCors。关于API中的具体参数含义,请参见PutBucketCors

--cors-configuration

--cors-configuration配置选项既支持XML语法也支持JSON语法:

  • XML语法:

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

    ...表示前面的节点支持多个。

  • JSON语法:

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

    JSON表示法时,会去掉XML的根节点。

说明

关于支持的全局命令行选项,请参见支持的全局命令行选项

使用示例

指定允许的跨域请求来源为www.aliyun.com、允许的跨域请求方法为PUT和GET,请求返回结果的缓存时间为10000秒。

  • 使用XML配置文件,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 
  • 使用JSON配置文件,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
  • 使用JSON配置参数:

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