设置托管配置
更新时间: 2019-03-14 10:05
您可以通过ObsClient.SetBucketWebsiteConfiguration设置桶的托管配置。
配置默认主页和错误页面
以下代码展示了如何配置默认主页和错误页面:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetBucketWebsiteConfigurationInput{}
- input.Bucket = "bucketname"
- // 配置默认主页
- input.IndexDocument.Suffix = "index.html"
- // 配置错误页面
- input.ErrorDocument.Key = "error.html"
- output, err := obsClient.SetBucketWebsiteConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
配置重定向规则
以下代码展示了如何配置重定向规则:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetBucketWebsiteConfigurationInput{}
- input.Bucket = "bucketname"
- // 配置默认主页
- input.IndexDocument.Suffix = "index.html"
- // 配置错误页面
- input.ErrorDocument.Key = "error.html"
- // 配置重定向规则
- var routingRules [2]obs.RoutingRule
- routingRule0 := obs.RoutingRule{}
- routingRule0.Redirect.HostName = "www.a.com"
- routingRule0.Redirect.Protocol = obs.ProtocolHttp
- routingRule0.Redirect.ReplaceKeyPrefixWith = "prefix"
- routingRule0.Redirect.HttpRedirectCode = "304"
- routingRules[0] = routingRule0
- routingRule1 := obs.RoutingRule{}
- routingRule1.Redirect.HostName = "www.b.com"
- routingRule1.Redirect.Protocol = obs.ProtocolHttps
- routingRule1.Redirect.ReplaceKeyWith = "replaceKey"
- routingRule1.Redirect.HttpRedirectCode = "304"
- routingRule1.Condition.HttpErrorCodeReturnedEquals = "404"
- routingRule1.Condition.KeyPrefixEquals = "prefix"
- routingRules[1] = routingRule1
- input.RoutingRules = routingRules[:]
- output, err := obsClient.SetBucketWebsiteConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
配置所有请求重定向
以下代码展示了如何配置所有请求重定向:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetBucketWebsiteConfigurationInput{}
- input.Bucket = "bucketname"
- input.RedirectAllRequestsTo.HostName = "www.a.com"
- input.RedirectAllRequestsTo.Protocol = obs.ProtocolHttp
- output, err := obsClient.SetBucketWebsiteConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:静态网站托管