全部产品
Search
文档中心

Object Storage Service:Mengonfigurasi aturan CORS

更新时间:Jul 02, 2025

Mengonfigurasi aturan berbagi sumber daya lintas domain (CORS) untuk sebuah bucket.

Sintaks perintah

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

Parameter

Tipe

Deskripsi

--bucket

string

Nama bucket yang ingin Anda konfigurasikan aturan CORS-nya.

--cors-configuration

string

Kontainer yang menyimpan aturan CORS dari bucket.

Catatan

Perintah put-bucket-cors sesuai dengan operasi PutBucketCors. Untuk informasi lebih lanjut tentang parameter dalam perintah, lihat PutBucketCors.

--cors-configuration

Opsi --cors-configuration mendukung format XML dan JSON:

  • Format XML:

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

    ... menunjukkan bahwa beberapa node didukung.

  • Format JSON:

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

    Node root XML dihapus dalam format JSON.

Catatan

Untuk informasi lebih lanjut, lihat Opsi baris perintah.

Contoh

Dalam contoh-contoh berikut, sumber permintaan lintas domain yang diizinkan adalah www.aliyun.com, metode permintaan lintas domain yang diizinkan adalah PUT dan GET, serta waktu respons cache adalah 10.000 detik.

  • Gunakan file konfigurasi XML bernama 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 
  • Gunakan file konfigurasi JSON bernama 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
  • Gunakan parameter konfigurasi JSON:

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