You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "github.com/tencentyun/cos-go-sdk-v5"
  8. "github.com/tencentyun/cos-go-sdk-v5/debug"
  9. )
  10. func main() {
  11. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  12. b := &cos.BaseURL{
  13. BucketURL: u,
  14. }
  15. c := cos.NewClient(b, &http.Client{
  16. Transport: &cos.AuthorizationTransport{
  17. SecretID: os.Getenv("COS_SECRETID"),
  18. SecretKey: os.Getenv("COS_SECRETKEY"),
  19. Transport: &debug.DebugRequestTransport{
  20. RequestHeader: true,
  21. RequestBody: true,
  22. ResponseHeader: true,
  23. ResponseBody: true,
  24. },
  25. },
  26. })
  27. opt := &cos.BucketPutWebsiteOptions{
  28. Index: "index.html",
  29. Error: &cos.ErrorDocument{"index_backup.html"},
  30. RedirectProtocol: &cos.RedirectRequestsProtocol{"https"},
  31. RoutingRules: &cos.WebsiteRoutingRules{
  32. []cos.WebsiteRoutingRule{
  33. {
  34. ConditionErrorCode: "404",
  35. RedirectProtocol: "https",
  36. RedirectReplaceKey: "404.html",
  37. },
  38. {
  39. ConditionPrefix: "docs/",
  40. RedirectProtocol: "https",
  41. RedirectReplaceKeyPrefix: "documents/",
  42. },
  43. },
  44. },
  45. }
  46. _, err := c.Bucket.PutWebsite(context.Background(), opt)
  47. if err != nil {
  48. panic(err)
  49. }
  50. }