设置托管配置

更新时间: 2019-03-14 10:05

您可以通过ObsClient.SetBucketWebsiteConfiguration设置桶的托管配置。

配置默认主页和错误页面

以下代码展示了如何配置默认主页和错误页面:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.SetBucketWebsiteConfigurationInput{}
  16. input.Bucket = "bucketname"
  17. // 配置默认主页
  18. input.IndexDocument.Suffix = "index.html"
  19. // 配置错误页面
  20. input.ErrorDocument.Key = "error.html"
  21. output, err := obsClient.SetBucketWebsiteConfiguration(input)
  22. if err == nil {
  23. fmt.Printf("RequestId:%s", output.RequestId)
  24. } else if obsError, ok := err.(obs.ObsError); ok {
  25. fmt.Printf("Code:%s\n", obsError.Code)
  26. fmt.Printf("Message:%s\n", obsError.Message)
  27. }
  28. }

配置重定向规则

以下代码展示了如何配置重定向规则:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.SetBucketWebsiteConfigurationInput{}
  16. input.Bucket = "bucketname"
  17. // 配置默认主页
  18. input.IndexDocument.Suffix = "index.html"
  19. // 配置错误页面
  20. input.ErrorDocument.Key = "error.html"
  21.  
  22. // 配置重定向规则
  23. var routingRules [2]obs.RoutingRule
  24. routingRule0 := obs.RoutingRule{}
  25.  
  26. routingRule0.Redirect.HostName = "www.a.com"
  27. routingRule0.Redirect.Protocol = obs.ProtocolHttp
  28. routingRule0.Redirect.ReplaceKeyPrefixWith = "prefix"
  29. routingRule0.Redirect.HttpRedirectCode = "304"
  30. routingRules[0] = routingRule0
  31.  
  32. routingRule1 := obs.RoutingRule{}
  33.  
  34. routingRule1.Redirect.HostName = "www.b.com"
  35. routingRule1.Redirect.Protocol = obs.ProtocolHttps
  36. routingRule1.Redirect.ReplaceKeyWith = "replaceKey"
  37. routingRule1.Redirect.HttpRedirectCode = "304"
  38.  
  39. routingRule1.Condition.HttpErrorCodeReturnedEquals = "404"
  40. routingRule1.Condition.KeyPrefixEquals = "prefix"
  41.  
  42. routingRules[1] = routingRule1
  43.  
  44. input.RoutingRules = routingRules[:]
  45. output, err := obsClient.SetBucketWebsiteConfiguration(input)
  46. if err == nil {
  47. fmt.Printf("RequestId:%s", output.RequestId)
  48. } else if obsError, ok := err.(obs.ObsError); ok {
  49. fmt.Printf("Code:%s\n", obsError.Code)
  50. fmt.Printf("Message:%s\n", obsError.Message)
  51. }
  52. }

配置所有请求重定向

以下代码展示了如何配置所有请求重定向:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.SetBucketWebsiteConfigurationInput{}
  16. input.Bucket = "bucketname"
  17. input.RedirectAllRequestsTo.HostName = "www.a.com"
  18. input.RedirectAllRequestsTo.Protocol = obs.ProtocolHttp
  19. output, err := obsClient.SetBucketWebsiteConfiguration(input)
  20. if err == nil {
  21. fmt.Printf("RequestId:%s", output.RequestId)
  22. } else if obsError, ok := err.(obs.ObsError); ok {
  23. fmt.Printf("Code:%s\n", obsError.Code)
  24. fmt.Printf("Message:%s\n", obsError.Message)
  25. }
  26. }

父主题:静态网站托管