All Products
Search
Document Center

Object Storage Service:cors

Last Updated:Aug 15, 2024

Cross-origin resource sharing (CORS) is a standard cross-origin solution that is provided by HTML5 to allow web application servers to control cross-origin access. This ensures the security of data transmission across origins. This topic describes how to run the cors command to add, modify, query, or delete CORS configurations for a bucket.

Usage notes

  • To create or modify a CORS configuration, you must have the oss:PutBucketCors permission. To query CORS configurations, you must have the oss:GetBucketCors permission. To delete a CORS configuration, you must have the oss:DeleteBucketCors permission. For more information, see Attach a custom policy to a RAM user.

  • For ossutil 1.6.16 and later, you can directly use ossutil as the binary name in the command line. You do not need to update the binary name based on the operating system. For ossutil earlier than 1.6.16, you need to update the binary name based on the operating system. For more information, see ossutil command reference.

  • For more information about CORS, see CORS.

Create or modify a CORS configuration for a bucket

If the bucket does not have an existing CORS configuration, the cors command with the --method put option included creates a CORS configuration for the bucket. If the bucket has an existing CORS configuration, the cors command with the --method put option included overwrites the existing CORS configuration for the bucket.

To create or modify a CORS configuration for a bucket, perform the following steps:

  1. Create a local file. Create a CORS configuration in the local file.

  2. Use ossutil to read the CORS configuration from the local file, and associate the CORS configuration with the specified bucket.

To create or modify a CORS configuration for a bucket, use the following command syntax:

  • Command syntax

    ossutil cors --method put oss://bucketname local_xml_file

    The following table describes the preceding parameters.

    Parameter

    Description

    bucketname

    The name of the bucket for which you want to create or modify the CORS configuration.

    local_xml_file

    The name of the local file where the CORS configuration is specified. Example: localfile.txt.

  • Examples

    1. Create a file named localfile.txt on the local computer and configure CORS in the file.

      The following sample CORS configuration allows cross-origin PUT requests from www.aliyun.com and specifies that the browser caches the response to a preflight (OPTIONS) request for 10,000 seconds.

      <?xml version="1.0" encoding="UTF-8"?>
         <CORSConfiguration>
           <CORSRule>
               <AllowedOrigin>www.aliyun.com</AllowedOrigin>
               <AllowedMethod>PUT</AllowedMethod>
               <MaxAgeSeconds>10000</MaxAgeSeconds>
           </CORSRule>
       </CORSConfiguration>
    2. Create the CORS configuration for the examplebucket bucket.

      ossutil cors --method put oss://examplebucket localfile.txt

      The following sample output indicates that the CORS configuration is created.

      0.299514(s) elapsed

Query the CORS configuration of a bucket

  • Command syntax

    ossutil cors --method get oss://bucketname [local_xml_file]

    The following table describes the preceding parameters.

    Parameter

    Description

    bucketname

    The name of the bucket whose CORS configuration you want to query.

    local_xml_file

    The name of the local file in which you want to store the CORS configuration. Example: localfile.txt. If this parameter is not specified, the CORS configuration is displayed on the screen.

  • Examples

    Run the following command to query the CORS configuration of a bucket named examplebucket:

    ossutil cors --method get oss://examplebucket localfile.txt

    The following sample output indicates that the CORS configuration is obtained and written to the localfile.txt file.

    0.212407(s) elapsed

Delete the CORS configuration of a bucket

  • Command syntax

    ossutil cors --method delete oss://bucketname
  • Examples

    Run the following command to delete the CORS configuration of the examplebucket bucket:

    ossutil cors --method delete oss://examplebucket

    The following sample output indicates that the CORS configuration of the examplebucket bucket is deleted.

    0.530750(s) elapsed

Common options

If you use ossutil to switch to a bucket that is located in another region, add the -e option to the command to specify the endpoint of the region in which the specified bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, add the -i option to the command to specify the AccessKey ID of the specified account, and add the -k option to the command to specify the AccessKey secret of the specified account.

For example, you can run the following command to create a CORS configuration for a bucket named testbucket, which is located in the China (Hangzhou) region and is owned by another Alibaba Cloud account:

ossutil cors --method put oss://testbucket localfile.txt -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA****  -k 67DLVBkH7EamOjy2W5RVAHUY9H****

For more information about common options, see Common options.