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.

52 lines
1.2 KiB

3 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "net/http"
  7. "github.com/tencentyun/cos-go-sdk-v5"
  8. "github.com/tencentyun/cos-go-sdk-v5/debug"
  9. )
  10. func log_status(err error) {
  11. if err == nil {
  12. return
  13. }
  14. if cos.IsNotFoundError(err) {
  15. // WARN
  16. fmt.Println("WARN: Resource is not existed")
  17. } else if e, ok := cos.IsCOSError(err); ok {
  18. fmt.Printf("ERROR: Code: %v\n", e.Code)
  19. fmt.Printf("ERROR: Message: %v\n", e.Message)
  20. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  21. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  22. // ERROR
  23. } else {
  24. fmt.Printf("ERROR: %v\n", err)
  25. // ERROR
  26. }
  27. }
  28. func main() {
  29. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  30. b := &cos.BaseURL{BucketURL: u}
  31. c := cos.NewClient(b, &http.Client{
  32. // 使用 CVMCredentialsTransport
  33. Transport: &cos.CVMCredentialsTransport{
  34. Transport: &debug.DebugRequestTransport{
  35. RequestHeader: true,
  36. // Notice when put a large file and set need the request body, might happend out of memory error.
  37. RequestBody: false,
  38. ResponseHeader: true,
  39. ResponseBody: false,
  40. },
  41. },
  42. })
  43. name := "exampleobject"
  44. _, err := c.Object.Get(context.Background(), name, nil)
  45. log_status(err)
  46. }