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.

61 lines
1.3 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strings"
  7. "io/ioutil"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  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 upload(c *cos.Client, name string) {
  29. f := strings.NewReader("test")
  30. f = strings.NewReader("test xxx")
  31. opt := &cos.ObjectPutOptions{
  32. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  33. ContentType: "text/html",
  34. },
  35. ACLHeaderOptions: &cos.ACLHeaderOptions{
  36. XCosACL: "public-read",
  37. },
  38. }
  39. c.Object.Put(context.Background(), name, f, opt)
  40. return
  41. }
  42. func main() {
  43. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  44. b := &cos.BaseURL{BucketURL: u}
  45. c := cos.NewClient(b, nil)
  46. name := "test/anonymous_get.go"
  47. upload(c, name)
  48. resp, err := c.Object.Get(context.Background(), name, nil)
  49. log_status(err)
  50. bs, _ := ioutil.ReadAll(resp.Body)
  51. defer resp.Body.Close()
  52. fmt.Printf("%s\n", string(bs))
  53. }