
# 设置桶的网站配置(Go SDK)
#### 功能介绍
OBS允许在桶内保存静态的网页资源，如.html网页文件、flash文件、音视频文件等，当客户端通过桶的Website接入点访问这些对象资源时，浏览器可以直接解析出这些支持的网页资源，呈现给最终用户。典型的应用场景有：
- 重定向所有的请求到另外一个站点。
- 设定特定的重定向规则来重定向特定的请求。
调用设置桶的网站配置接口，您可以为指定桶设置网站配置信息。
#### 接口约束
- 尽量避免目标桶名中带有"."，否则通过HTTPS访问时可能出现客户端校验证书出错。
- 设置桶的网站配置请求消息体的上限是10KB。
- 您必须是桶拥有者或拥有设置桶的网站配置的权限，才能设置桶的网站配置。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:PutBucketWebsite权限，如果使用桶策略则需授予PutBucketWebsite权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://developer.huaweicloud.com/endpoint?OBS)。
 
#### 方法定义
**func** (obsClient ObsClient) SetBucketWebsiteConfiguration(input \*[SetBucketWebsiteConfigurationInput], extensions...extensionOptions) (output \*[BaseModel], err error)
#### 请求参数说明
表1请求参数列表 
| **参数名称**   | **参数类型**                                                                             | **是否必选** | **描述**                                                                                                                                                             |
|:---|:---|:---|:---|
| input      | \*[SetBucketWebsiteConfigurationInput]                  | 必选       | **参数解释：** 设置桶的网站配置请求参数，详见[SetBucketWebsiteConfigurationInput]。                                         |
| extensions | [extensionOptions](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0105.html) | 可选       | **参数解释：** 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头，详情参考[extensionOptions](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0105.html)。 |
   
 表2SetBucketWebsiteConfigurationInput 
| **参数名称**              | **参数类型**                                                 | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| Bucket                | string                                                   | 必选       | **参数解释** **：** 桶名。 **约束限制：** - 桶命名规则如下： - 需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。用户删除桶后，立即创建同名桶或并行文件系统会创建失败，需要等待30分钟才能创建。  - 长度范围为3到63个字符，仅支持小写字母、数字、中划线（-）、英文句号（.）。  - 禁止两个英文句号（.）相邻，禁止英文句号（.）和中划线（-）相邻，禁止以英文句号（.）和中划线（-）开头或结尾。  - 禁止使用IP地址。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **取值范围：** 长度为3～63个字符。 |
| RedirectAllRequestsTo | [RedirectAllRequestTo] | 可选       | **参数解释：** 所有请求重定向规则，详见[RedirectAllRequestTo]。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| IndexDocument         | [IndexDocument]         | 可选       | **参数解释：** 默认页面配置，详见[IndexDocument]。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ErrorDocument         | [ErrorDocument]        | 可选       | **参数解释：** 错误页面配置，详见[ErrorDocument]。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| RoutingRules          | \[\][RoutingRule]      | 可选       | **参数解释：** 请求重定向规则列表，详见[RoutingRule]。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
   
![](https://support.huaweicloud.com/sdk-go-devg-obs/public_sys-resources/note_3.0-zh-cn.png)
- ErrorDocument，IndexDocument和RoutingRules必须配套使用，且与RedirectAllRequestsTo互斥。当设置了这三个字段时，不能设置RedirectAllRequestsTo；反之，当设置了RedirectAllRequestsTo时，不能设置ErrorDocument，IndexDocument和RoutingRules。
- 当ErrorDocument，IndexDocument和RoutingRules三个字段一起使用时，RoutingRules可为空。
- ErrorDocument，IndexDocument、RoutingRules与RedirectAllRequestsTo不能全为空。
 
 表3RedirectAllRequestsTo 
| **字段名**  | **类型**                                         | **约束**                       | **说明**                                                                                                                                                                                                                                                        |
|:---|:---|:---|:---|
| HostName | string                                         | 如果RedirectAllRequestsTo 设置必选 | **参数解释：** 重定向时使用的域名。如 www.example.com。 **约束限制：** 域名需符合域名规范。 **默认取值：** 无         |
| Protocol | [ProtocolType] | 可选                           | **参数解释：** 重定向请求时使用的协议。 **取值范围：** 协议类型可选范围参见[ProtocolType]。 **默认取值：** 无 |
   
 表4IndexDocument 
| **参数名称** | **参数类型** | **是否必选**                 | **描述**                                                                                                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| Suffix   | string   | 如果IndexDocument 设置则参数时必选 | **参数解释：** 该字段被追加在对文件夹的请求的末尾（例如：Suffix参数设置为"index.html"，请求的是"samplebucket/images/"，返回的数据将是"samplebucket"桶内名为"images/index.html"的对象的内容）。 **取值范围：** 该字段不能为空或者包含"/"字符。 **默认取值：** 无 |
   
 表5ErrorDocument 
| **参数名称** | **参数类型** | **是否必选**              | **描述**                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| Key      | string   | 如果ErrorDocument 设置时可选 | **参数解释：** 当4XX错误出现时使用的对象的名称。这个元素指定当错误出现时返回的页面。 **取值范围：** 长度大于0且不超过1024的字符串。 **默认取值：** 无 |
   
 表6RoutingRule 
| **字段名**   | **类型**                                        | **约束**                 | **说明**                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| Condition | [Condition]  | 可选                     | **参数解释：** 重定向规则生效需要满足的条件。 **取值范围：** 匹配条件选择范围详见[Condition]。 **默认取值：** 无 |
| Redirect  | [Redirect] | 如果RoutingRule 设置则参数时必选 | **参数解释：** 重定向请求时的具体信息，详见[Redirect]。                                                                                                                                                      |
   
 表7Condition 
| **参数名称**                    | **参数类型** | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| 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参数不可同时使用，两者二选一。 **取值范围：** 取值范围可参见[错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html)。 **默认取值：** 无 |
   
 表8Redirect 
| **参数名称**             | **参数类型**                                       | **是否必选**  | **描述**                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| Protocol             | [ProtocolType] | 作为请求参数时可选 | **参数解释：** 重定向请求时使用的协议。 **取值范围：** 协议类型可选范围参见[ProtocolType]。 **默认取值：** 无                                                        |
| HostName             | string                                         | 作为请求参数时可选 | **参数解释：** 重定向请求时使用的域名。 **默认取值：** 无                                                                                                                                                                                           |
| ReplaceKeyPrefixWith | string                                         | 作为请求参数时可选 | **参数解释：** 重定向请求时使用的对象名前缀。 **取值范围：** 长度大于0且不超过1024的字符串。 **默认取值：** 无                                                                                                |
| ReplaceKeyWith       | string                                         | 作为请求参数时可选 | **参数解释：** 重定向请求时使用的对象名。 **约束限制：** 不可与replaceKeyPrefixWith同时使用。 **取值范围：** 长度大于0且不超过1024的字符串。 **默认取值：** 无 |
| HttpRedirectCode     | string                                         | 作为请求参数时可选 | **参数解释：** 重定向请求时响应中的HTTP状态码，详见[状态码](https://support.huaweicloud.com/api-obs/obs_04_0114.html)。 **默认取值：** 无                                                                                                                   |
   
 表9ProtocolType 
| **常量名**       | **原始值** | **说明**           |
|:---|:---|:---|
| ProtocolHttp  | http    | 重定向请求时使用HTTP协议。  |
| ProtocolHttps | https   | 重定向请求时使用HTTPS协议。 |
   
#### 返回结果说明
表10返回结果列表 
| **参数名称** | **参数类型**                                     | **描述**                                                                                        |
|:---|:---|:---|
| output   | \*[BaseModel] | **参数解释：** 接口返回信息，详见[BaseModel]。 |
| err      | error                                        | **参数解释：** 接口返回错误信息。                                           |
   
 表11BaseModel 
| **参数名称**        | **参数类型**                | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| StatusCode      | int                     | **参数解释：** HTTP状态码。 **约束限制：** 无 **取值范围：** 状态码是一组从2xx（成功）到4xx或5xx（错误）的数字代码，状态码表示了请求响应的状态。完整的状态码列表请参见[状态码](https://support.huaweicloud.com/api-obs/obs_04_0114.html)。 **默认取值：** 无 |
| RequestId       | string                  | **参数解释：** OBS服务端返回的请求ID。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                      |
| ResponseHeaders | map\[string\]\[\]string | **参数解释：** HTTP响应头信息。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                      |
   
#### 代码示例
本示例用于配置名为examplebucket桶的网站信息。
```
package main
import (
    "fmt"
    "os"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    //推荐通过环境变量获取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))
    if err != 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)
    if err == 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)
    if obsError, 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)
    }
}
```
#### 相关链接
- 关于设置桶的网站配置的API说明，请参见[设置桶的网站配置](https://support.huaweicloud.com/api-obs/obs_04_0071.html)。
- 更多关于设置桶的Website配置的示例代码，请参见[Github示例](https://github.com/huaweicloud/huaweicloud-sdk-go-obs/blob/master/examples/bucket_operations_sample.go)。
- 设置桶的Website配置过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
- 静态网站托管相关常见问题请参见[静态网站托管相关常见问题](https://support.huaweicloud.com/obs_faq/obs_faq_0500.html)。
 
