bucket_website.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketWebsiteSample shows how to set, get and delete the bucket website.
  7. func BucketWebsiteSample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create the bucket with default parameters
  14. err = client.CreateBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. //Define bucket website indexWebsite or errorWebsite
  19. var indexWebsite = "myindex.html"
  20. var errorWebsite = "myerror.html"
  21. // Set bucket website indexWebsite or errorWebsite
  22. err = client.SetBucketWebsite(bucketName, indexWebsite, errorWebsite)
  23. if err != nil {
  24. HandleError(err)
  25. }
  26. bEnable := true
  27. bDisable := false
  28. // Define one website detail
  29. ruleOk := oss.RoutingRule{
  30. RuleNumber: 1,
  31. Condition: oss.Condition{
  32. KeyPrefixEquals: "abc",
  33. HTTPErrorCodeReturnedEquals: 404,
  34. IncludeHeader: []oss.IncludeHeader{
  35. oss.IncludeHeader{
  36. Key: "host",
  37. Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
  38. },
  39. },
  40. },
  41. Redirect: oss.Redirect{
  42. RedirectType: "Mirror",
  43. PassQueryString: &bDisable,
  44. MirrorURL: "http://www.test.com/",
  45. MirrorPassQueryString: &bEnable,
  46. MirrorFollowRedirect: &bEnable,
  47. MirrorCheckMd5: &bDisable,
  48. MirrorHeaders: oss.MirrorHeaders{
  49. PassAll: &bEnable,
  50. Pass: []string{"key1", "key2"},
  51. Remove: []string{"remove1", "remove2"},
  52. Set: []oss.MirrorHeaderSet{
  53. oss.MirrorHeaderSet{
  54. Key: "setKey1",
  55. Value: "setValue1",
  56. },
  57. },
  58. },
  59. },
  60. }
  61. wxmlDetail := oss.WebsiteXML{}
  62. wxmlDetail.RoutingRules = append(wxmlDetail.RoutingRules, ruleOk)
  63. // Get website
  64. res, err := client.GetBucketWebsite(bucketName)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. fmt.Println("Website IndexDocument:", res.IndexDocument.Suffix)
  69. // Set bucket website detail
  70. err = client.SetBucketWebsiteDetail(bucketName, wxmlDetail)
  71. if err != nil {
  72. HandleError(err)
  73. }
  74. // Get website Detail
  75. res, err = client.GetBucketWebsite(bucketName)
  76. if err != nil {
  77. HandleError(err)
  78. }
  79. fmt.Println("Website Redirect type:", res.RoutingRules[0].Redirect.RedirectType)
  80. // Delete Website
  81. err = client.DeleteBucketWebsite(bucketName)
  82. if err != nil {
  83. HandleError(err)
  84. }
  85. // Delete bucket
  86. err = client.DeleteBucket(bucketName)
  87. if err != nil {
  88. HandleError(err)
  89. }
  90. fmt.Println("BucketWebsiteSample completed")
  91. }