Due to the same-origin policy of browsers, cross-origin requests may be rejected when data is exchanged or resources are shared between different domain names. To resolve the issue, you can configure cross-origin resource sharing (CORS) rules. In the CORS rules, you can specify the domain names from which requests can be sent, the methods that can be used to send cross-origin requests, and the allowed headers.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To configure CORS rules, you must have the
oss:PutBucketCors
permission. To query CORS rules, you must have theoss:GetBucketCors
permission. To delete CORS rules, you must have theoss:DeleteBucketCors
permission. For more information, see Attach a custom policy to a RAM user.
Sample Code
Configure CORS rules
Query CORS rules
Delete CORS rules
References
For the complete sample code that is used to manage CORS rules, visit GitHub.
For more information about the API operation that you can call to configure CORS rules, see SetBucketCORSV2.
For more information about the API operation that you can call to query CORS rules, see GetBucketCORS.
For more information about the API operation that you can call to delete CORS rules, see DeleteBucketCORS.