设置桶的网站配置(go sdk)-九游平台
功能介绍
obs允许在桶内保存静态的网页资源,如.html网页文件、flash文件、音视频文件等,当客户端通过桶的website接入点访问这些对象资源时,浏览器可以直接解析出这些支持的网页资源,呈现给最终用户。典型的应用场景有:
- 重定向所有的请求到另外一个站点。
- 设定特定的重定向规则来重定向特定的请求。
调用设置桶的网站配置接口,您可以为指定桶设置网站配置信息。
接口约束
- 尽量避免目标桶名中带有“.”,否则通过https访问时可能出现客户端校验证书出错。
- 设置桶的网站配置请求消息体的上限是10kb。
- 您必须是桶拥有者或拥有设置桶的网站配置的权限,才能设置桶的网站配置。建议使用iam或桶策略进行授权,如果使用iam则需授予obs:bucket:putbucketwebsite权限,如果使用桶策略则需授予putbucketwebsite权限。相关授权方式介绍可参见obs权限控制概述,配置方式详见、。
- obs支持的region与endpoint的对应关系,详细信息请参见。
方法定义
func (obsclient obsclient) setbucketwebsiteconfiguration(input *setbucketwebsiteconfigurationinput, extensions...extensionoptions) (output *basemodel, err error)
请求参数说明
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
input |
必选 |
参数解释: 设置桶的网站配置请求参数,详见setbucketwebsiteconfigurationinput。 |
|
extensions |
可选 |
参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考。 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
bucket |
string |
必选 |
参数解释: 桶名。 约束限制:
取值范围: 长度为3~63个字符。 |
redirectallrequeststo |
可选 |
参数解释: 所有请求重定向规则,详见redirectallrequestto。 |
|
indexdocument |
可选 |
参数解释: 默认页面配置,详见indexdocument。 |
|
errordocument |
可选 |
参数解释: 错误页面配置,详见errordocument。 |
|
routingrules |
可选 |
参数解释: 请求重定向规则列表,详见routingrule。 |

- errordocument,indexdocument和routingrules必须配套使用,且与redirectallrequeststo互斥。当设置了这三个字段时,不能设置redirectallrequeststo;反之,当设置了redirectallrequeststo时,不能设置errordocument,indexdocument和routingrules。
- 当errordocument,indexdocument和routingrules三个字段一起使用时,routingrules可为空。
- errordocument,indexdocument、routingrules与redirectallrequeststo不能全为空。
字段名 |
类型 |
约束 |
说明 |
---|---|---|---|
hostname |
string |
如果redirectallrequeststo 设置必选 |
参数解释: 重定向时使用的域名。如 www.example.com。 约束限制: 域名需符合域名规范。 默认取值: 无 |
protocol |
可选 |
参数解释: 重定向请求时使用的协议。 取值范围: 协议类型可选范围参见protocoltype。 默认取值: 无 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
suffix |
string |
如果indexdocument 设置则参数时必选 |
参数解释: 该字段被追加在对文件夹的请求的末尾(例如:suffix参数设置为“index.html”,请求的是“samplebucket/images/”,返回的数据将是“samplebucket”桶内名为“images/index.html”的对象的内容)。 取值范围: 该字段不能为空或者包含“/”字符。 默认取值: 无 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
key |
string |
如果errordocument 设置时可选 |
参数解释: 当4xx错误出现时使用的对象的名称。这个元素指定当错误出现时返回的页面。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
字段名 |
类型 |
约束 |
说明 |
---|---|---|---|
condition |
可选 |
参数解释: 重定向规则生效需要满足的条件。 取值范围: 匹配条件选择范围详见condition。 默认取值: 无 |
|
redirect |
如果routingrule 设置则参数时必选 |
参数解释: 重定向请求时的具体信息,详见redirect。 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
keyprefixequals |
string |
可选 |
参数解释: 重定向生效时的对象名前缀。当向对象发送请求时,如果对象名前缀等于这个值,那么重定向生效。 例如:重定向examplepage.html对象的请求,keyprefixequals设为examplepage.html。 约束限制: 与httperrorcodereturnedequals参数不可同时使用,两者二选一。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
httperrorcodereturnedequals |
string |
可选 |
参数解释: 重定向生效时的http错误码。当发生错误时,如果错误码等于这个值,那么重定向生效。 例如:当返回的http错误码为404时重定向到notfound.html,可以将condition中的httperrorcodereturnedequals设置为404,redirect中的replacekeywith设置为notfound.html。 约束限制: 与keyprefixequals参数不可同时使用,两者二选一。 取值范围: 取值范围可参见错误码。 默认取值: 无 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
protocol |
作为请求参数时可选 |
参数解释: 重定向请求时使用的协议。 取值范围: 协议类型可选范围参见protocoltype。 默认取值: 无 |
|
hostname |
string |
作为请求参数时可选 |
参数解释: 重定向请求时使用的域名。 默认取值: 无 |
replacekeyprefixwith |
string |
作为请求参数时可选 |
参数解释: 重定向请求时使用的对象名前缀。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
replacekeywith |
string |
作为请求参数时可选 |
参数解释: 重定向请求时使用的对象名。 约束限制: 不可与replacekeyprefixwith同时使用。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
httpredirectcode |
string |
作为请求参数时可选 |
参数解释: 重定向请求时响应中的http状态码,详见。 默认取值: 无 |
常量名 |
原始值 |
说明 |
---|---|---|
protocolhttp |
http |
重定向请求时使用http协议。 |
protocolhttps |
https |
重定向请求时使用https协议。 |
返回结果说明
参数名称 |
参数类型 |
描述 |
---|---|---|
statuscode |
int |
参数解释: http状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见。 默认取值: 无 |
requestid |
string |
参数解释: obs服务端返回的请求id。 默认取值: 无 |
responseheaders |
map[string][]string |
参数解释: http响应头信息。 默认取值: 无 |
代码示例
本示例用于配置名为examplebucket桶的网站信息。
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 |
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.setbucketwebsiteconfigurationinput{} // 指定存储桶名称 input.bucket="examplebucket" // 指定默认页面配置,此处以index.html为例,含义是请求examplebucket/images/时,返回的数据将是samplebucket桶内名为images/index.html的对象的内容 input.indexdocument.suffix="index.html" // 指定错误页面配置,此处以error.html为例 input.errordocument.key="error.html" // 指定请求重定向规则列表 input.routingrules=[]obs.routingrule{ {redirect:obs.redirect{hostname:"www.a.com",protocol:obs.protocolhttp,replacekeyprefixwith:"prefix",httpredirectcode:"304"}}, {redirect:obs.redirect{hostname:"www.b.com",protocol:obs.protocolhttps,replacekeywith:"replacekey",httpredirectcode:"304"}}, } // 设置桶的website配置 output,err:=obsclient.setbucketwebsiteconfiguration(input) iferr==nil{ fmt.printf("set bucket(%s)'s website successful!\n",input.bucket) fmt.printf("requestid:%s\n",output.requestid) return } fmt.printf("set bucket(%s)'s website 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) } } |
相关文档
意见反馈
文档内容是否对您有帮助?
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨