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. This topic describes how to resolve related issues by configuring cross-origin resource sharing (CORS) rules, in which 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
The sample code in this topic uses the region ID
cn-hangzhou
of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
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 Grant custom policy to RAM users.
Sample code
References
For more information about the complete sample code of CORS, visit put_bucket_cors.go, get_bucket_cors.go, and delete_bucket_cors.go.
For more information about the API operation that you can call to configure CORS rules, see PutBucketCors.
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.