Cross-origin resource sharing (CORS) is a standard HTML5 mechanism that allows web application servers to control cross-origin access. This ensures the security of data transmission across different origins. This topic describes how to use the cors command to add, modify, query, and delete a bucket's CORS configuration.
Usage notes
-
To add or modify a CORS configuration, you must have the
oss:PutBucketCorspermission. To retrieve a CORS configuration, you must have theoss:GetBucketCorspermission. To delete a CORS configuration, you must have theoss:DeleteBucketCorspermission. For more information, see Grant custom permissions to a RAM user.
-
Starting from ossutil 1.6.16, you can use ossutil as the binary name in the command line regardless of your operating system. If you use a version of ossutil earlier than 1.6.16, you must change the binary name based on your operating system. For more information, see ossutil command reference.
-
For more information about CORS, see Cross-origin resource sharing.
Add or modify a CORS configuration
If the bucket does not have a CORS configuration, this command adds one. If the bucket already has a CORS configuration, this command overwrites it.
To add or modify a CORS configuration, follow these steps:
-
Create a local file and add CORS rules to the file.
-
ossutil reads the CORS configuration from the local file and applies it to the specified bucket.
The command format and an example are as follows:
-
Command format
ossutil cors --method put oss://bucketname local_xml_fileThe parameters are as follows:
Parameter
Description
bucketname
The name of the bucket for which you want to add or modify the CORS configuration.
local_xml_file
The name of the local file that contains the CORS rules. For example,
localfile.txt. -
Example
-
Create a local file named
localfile.txtand add the required CORS rules to the file.The following sample CORS rule allows cross-origin
PUTrequests fromhttp://www.aliyun.com. It also specifies that the browser caches the response to a preflight (OPTIONS) request for a specific resource for 10,000 seconds.<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration> <CORSRule> <AllowedOrigin>http://www.aliyun.com</AllowedOrigin> <AllowedMethod>PUT</AllowedMethod> <MaxAgeSeconds>10000</MaxAgeSeconds> </CORSRule> </CORSConfiguration> -
Add a CORS rule for the examplebucket bucket.
ossutil cors --method put oss://examplebucket localfile.txtThe following output indicates that the CORS rule is added.
0.299514(s) elapsed
-
Get a CORS configuration
-
Command format
ossutil cors --method get oss://bucketname [local_xml_file]The parameters are as follows:
Parameter
Description
bucketname
The name of the bucket whose CORS configuration you want to get.
local_xml_file
The name of the local file to which you want to save the CORS configuration. For example,
localfile.txt. If you do not specify this parameter, the CORS configuration is printed to the screen. -
Example
Retrieve the CORS configuration for the examplebucket bucket.
ossutil cors --method get oss://examplebucket localfile.txtThe following output indicates that the CORS configuration is retrieved and written to the localfile.txt file.
0.212407(s) elapsed
Delete a CORS configuration
-
Command format
ossutil cors --method delete oss://bucketname -
Example
Delete the CORS configuration from the examplebucket bucket.
ossutil cors --method delete oss://examplebucketThe following output indicates that the CORS configuration for the examplebucket bucket is deleted.
0.530750(s) elapsed
Common options
To access a bucket in a different region, use -e to specify the endpoint. To access a bucket owned by a different Alibaba Cloud account, use -i for the AccessKey ID and -k for the AccessKey secret.
For example, to add a CORS configuration to a bucket named testbucket that is in the China (Hangzhou) region and owned by another Alibaba Cloud account, run the following command:
ossutil cors --method put oss://testbucket localfile.txt -e oss-cn-hangzhou.aliyuncs.com -i yourAccessKeyID -k yourAccessKeySecret