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.

71 lines
1.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  9. "github.com/tencentyun/cos-go-sdk-v5/debug"
  10. )
  11. func log_status(err error) {
  12. if err == nil {
  13. return
  14. }
  15. if cos.IsNotFoundError(err) {
  16. // WARN
  17. fmt.Println("Resource is not existed")
  18. } else if e, ok := cos.IsCOSError(err); ok {
  19. fmt.Printf("Code: %v\n", e.Code)
  20. fmt.Printf("Message: %v\n", e.Message)
  21. fmt.Printf("Resource: %v\n", e.Resource)
  22. fmt.Printf("RequestId: %v\n", e.RequestID)
  23. // ERROR
  24. } else {
  25. fmt.Println(err)
  26. // ERROR
  27. }
  28. }
  29. func main() {
  30. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  31. b := &cos.BaseURL{
  32. BucketURL: u,
  33. }
  34. c := cos.NewClient(b, &http.Client{
  35. Transport: &cos.AuthorizationTransport{
  36. SecretID: os.Getenv("COS_SECRETID"),
  37. SecretKey: os.Getenv("COS_SECRETKEY"),
  38. Transport: &debug.DebugRequestTransport{
  39. RequestHeader: true,
  40. RequestBody: true,
  41. ResponseHeader: true,
  42. ResponseBody: true,
  43. },
  44. },
  45. })
  46. res, _, err := c.Bucket.GetAccelerate(context.Background())
  47. log_status(err)
  48. fmt.Printf("%+v\n", res)
  49. opt := &cos.BucketPutAccelerateOptions{
  50. Status: "Enabled",
  51. }
  52. _, err = c.Bucket.PutAccelerate(context.Background(), opt)
  53. log_status(err)
  54. res, _, err = c.Bucket.GetAccelerate(context.Background())
  55. log_status(err)
  56. fmt.Printf("%+v\n", res)
  57. opt.Status = "Suspended"
  58. _, err = c.Bucket.PutAccelerate(context.Background(), opt)
  59. log_status(err)
  60. res, _, err = c.Bucket.GetAccelerate(context.Background())
  61. log_status(err)
  62. fmt.Printf("%+v\n", res)
  63. }