查看PDF

设置桶的CORS配置

更新时间: 2019-03-13 10:26

功能说明

设置桶的跨域资源共享规则,以允许客户端浏览器进行跨域请求。

方法定义

  1. func (obsClient ObsClient) SetBucketCors(input *SetBucketCorsInput) (output *BaseModel, err error)

使用带授权信息URL的方法定义

  1. func (obsClient ObsClient) SetBucketCorsWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header, data io.Reader) (output *BaseModel, err error)

请求参数


参数名

类型

约束

input

*SetBucketCorsInput

必选

返回结果


参数名

类型

output

*BaseModel

err

error

代码样例

  1. func main() {
  2. input := &obs.SetBucketCorsInput{}
  3. input.Bucket = "bucketname"
  4. var corsRules [2]obs.CorsRule
  5. corsRule0 := obs.CorsRule{}
  6. corsRule0.ID = "rule1"
  7. corsRule0.AllowedOrigin = []string{"http://www.a.com", "http://www.b.com"}
  8. corsRule0.AllowedMethod = []string{"GET", "PUT", "POST", "HEAD"}
  9. corsRule0.AllowedHeader = []string{"header1", "header2"}
  10. corsRule0.MaxAgeSeconds = 100
  11. corsRule0.ExposeHeader = []string{"obs-1", "obs-2"}
  12. corsRules[0] = corsRule0
  13. corsRule1 := obs.CorsRule{}
  14.  
  15. corsRule1.ID = "rule2"
  16. corsRule1.AllowedOrigin = []string{"http://www.c.com", "http://www.d.com"}
  17. corsRule1.AllowedMethod = []string{"GET", "PUT", "POST", "HEAD"}
  18. corsRule1.AllowedHeader = []string{"header3", "header4"}
  19. corsRule1.MaxAgeSeconds = 50
  20. corsRule1.ExposeHeader = []string{"obs-3", "obs-4"}
  21. corsRules[1] = corsRule1
  22. input.CorsRules = corsRules[:]
  23. output, err := obsClient.SetBucketCors(input)
  24. if err == nil {
  25. fmt.Printf("RequestId:%s\n", output.RequestId)
  26. } else {
  27. if obsError, ok := err.(obs.ObsError); ok {
  28. fmt.Println(obsError.Code)
  29. fmt.Println(obsError.Message)
  30. } else {
  31. fmt.Println(err)
  32. }
  33. }
  34. }

父主题:桶相关接口