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.
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 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.
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
To configure CORS rules, you must have the
oss:PutBucketCorspermission. To query CORS rules, you must have theoss:GetBucketCorspermission. To delete CORS rules, you must have theoss:DeleteBucketCorspermission. For more information, see Attach a custom policy to a RAM user.
Sample code
References
For the complete sample code for CORS, see GitHub sample.
For more information about the API operation used to set CORS rules, see SetBucketCORSV2.
For more information about the API operation used to retrieve CORS rules, see GetBucketCORS.
For more information about the API operation used to delete CORS rules, see DeleteBucketCORS.