The same-origin policy in browsers prevents cross-origin requests when data is exchanged or resources are shared between different domain names. This topic describes how to resolve these cross-domain issues by setting a cross-origin access policy that allows access from specified domain names, using specified methods, and with specified request headers.
Usage notes
The sample code in this topic uses the China (Hangzhou) region with the ID
cn-hangzhou
as an example. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud products in the same region, you can use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.The examples in this topic show how to obtain access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To set cross-origin rules, you must have the
oss:PutBucketCors
permission. To query cross-origin rules, you must have theoss:GetBucketCors
permission. To delete cross-origin rules, you must have theoss:DeleteBucketCors
permission. For more information, see Grant custom access policies to RAM users.
Sample code
References
For the complete sample code for cross-origin resource sharing, see 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 set cross-origin rules, see PutBucketCors.
For more information about the API operation that you can call to query cross-origin rules, see GetBucketCORS.
For more information about the API operation that you can call to delete cross-origin rules, see DeleteBucketCORS.