设置桶的cors配置(go sdk)-九游平台
功能介绍
跨域资源共享(cross origin resource sharing,cors)是由w3c标准化组织提出的一种网络浏览器的规范机制,定义了一个域中加载的客户端web应用程序与另一个域中的资源交互的方式。而在通常的网页请求中,由于同源安全策略(same origin policy,sop)的存在,不同域之间的网站脚本和内容是无法进行交互的。obs支持cors规范,允许跨域请求访问obs中的资源。
调用设置桶的cors配置接口,您可设置指定桶的跨域资源共享规则,以允许客户端浏览器进行跨域请求。
接口约束
- 您必须是桶拥有者或拥有设置桶的cors配置的权限,才能设置桶的cors配置。建议使用iam或桶策略进行授权,如果使用iam则需授予obs:bucket:putbucketcors权限,如果使用桶策略则需授予putbucketcors权限。相关授权方式介绍可参见obs权限控制概述,配置方式详见、。
- obs支持的region与endpoint的对应关系,详细信息请参见。
方法定义
func (obsclient obsclient) setbucketcors(input *setbucketcorsinput, extensions...extensionoptions) (output *basemodel, err error)
请求参数说明
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
input |
必选 |
参数解释: 设置桶的cors配置请求参数,详见setbucketcorsinput。 |
|
extensions |
可选 |
参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考。 |
参数 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
bucket |
string |
必选 |
参数解释: 桶名。 约束限制:
默认取值: 无 |
corsrules |
[]corsrule |
必选 |
参数解释: 桶的cors规则列表,详见corsrule。 约束限制: 列表最多包含100个规则。 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
id |
string |
作为请求参数时可选 |
参数解释: cors规则id 取值范围: 长度大于0且不超过255的字符串。 默认取值: 无 |
allowedmethod |
[]string |
作为请求参数时必选 |
参数解释: 指定允许的跨域请求http方法,即桶和对象的几种操作类型。 取值范围: 支持以下http方法:
默认取值: 无 |
allowedorigin |
[]string |
作为请求参数时必选 |
参数解释: 指定允许的跨域请求的来源,即允许来自该域名下的请求访问该桶。 约束限制: 表示域名的字符串,每个匹配规则允许使用最多一个“*”通配符。例如:https://*.vbs.example.com。 默认取值: 无 |
allowedheader |
[]string |
作为请求参数时可选 |
参数解释: 指定允许的跨域请求的头域。只有匹配上允许的头域中的配置,才被视为是合法的cors请求。 约束限制: 最多可填写一个“*”通配符,不支持&、:、<、空格以及中文字符。 默认取值: 无 |
maxageseconds |
int |
作为请求参数时可选 |
参数解释: 请求来源的客户端可以对跨域请求返回结果的缓存时间。 约束限制: 每个corsrule可以包含至多一个maxageseconds。 取值范围: 0~(231-1),单位:秒。 默认取值: 100 |
exposeheader |
[]string |
作为请求参数时可选 |
参数解释: cors规则允许响应中可返回的附加头域,给客户端提供额外的信息。默认情况下浏览器只能访问以下头域:content-length、content-type,如果需要访问其他头域,需要在附加头域中配置。 约束限制: 不支持*、&、:、<、空格以及中文字符。 默认取值: 无 |
返回结果说明
参数名称 |
参数类型 |
描述 |
---|---|---|
statuscode |
int |
参数解释: http状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见。 默认取值: 无 |
requestid |
string |
参数解释: obs服务端返回的请求id。 默认取值: 无 |
responseheaders |
map[string][]string |
参数解释: http响应头信息。 默认取值: 无 |
代码示例
本示例用于配置examplebucket桶的cors规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
packagemain import( "fmt" "os" obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) funcmain(){ //推荐通过环境变量获取aksk,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。 //您可以登录访问管理控制台获取访问密钥ak/sk,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html。 ak:=os.getenv("accesskeyid") sk:=os.getenv("secretaccesskey") // 【可选】如果使用临时ak/sk和securitytoken访问obs,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥ak/sk,也可以使用其他外部引入方式传入。 securitytoken:=os.getenv("securitytoken") // endpoint填写bucket对应的endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。 endpoint:="https://obs.cn-north-4.myhuaweicloud.com" // 创建obsclient实例 // 如果使用临时aksk和securitytoken访问obs,需要在创建实例时通过obs.withsecuritytoken方法指定securitytoken值。 obsclient,err:=obs.new(ak,sk,endpoint,obs.withsecuritytoken(securitytoken)) iferr!=nil{ fmt.printf("create obsclient error, errmsg: %s",err.error()) } input:=&obs.setbucketcorsinput{} // 指定存储桶名称 input.bucket="examplebucket" // 指定cors规则 input.corsrules=[]obs.corsrule{ { id:"rule1", allowedorigin:[]string{"http://www.a.com","http://www.b.com"}, allowedmethod:[]string{"get","put"}, allowedheader:[]string{"header1","header2"}, maxageseconds:1000, exposeheader:[]string{"obs-1","obs-2"}, }, { id:"rule2", allowedorigin:[]string{"http://www.c.com","http://www.d.com"}, allowedmethod:[]string{"get","post"}, allowedheader:[]string{"header3","header4"}, maxageseconds:1000, }, } // 设置桶的cors配置 output,err:=obsclient.setbucketcors(input) iferr==nil{ fmt.printf("set bucket(%s) cors configuration successful!\n",input.bucket) fmt.printf("requestid:%s\n",output.requestid) return } fmt.printf("set bucket(%s) cors configuration fail!\n",input.bucket) ifobserror,ok:=err.(obs.obserror);ok{ fmt.println("an obserror was found, which means your request sent to obs was rejected with an error response.") fmt.println(obserror.error()) }else{ fmt.println("an exception was found, which means the client encountered an internal problem when attempting to communicate with obs, for example, the client was unable to access the network.") fmt.println(err) } } |
相关链接
- 关于设置桶的cors配置的api说明,请参见设置桶的cors配置。
- 更多关于设置桶的cors配置的示例代码,请参见。
- 设置桶的cors配置过程中返回的错误码含义、问题原因及处理措施可参考obs错误码。
相关文档
意见反馈
文档内容是否对您有帮助?
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨