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.

54 lines
1.1 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "io/ioutil"
  8. "net/http"
  9. "github.com/tencentyun/cos-go-sdk-v5"
  10. "github.com/tencentyun/cos-go-sdk-v5/debug"
  11. )
  12. func main() {
  13. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  14. b := &cos.BaseURL{BucketURL: u}
  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. name := "test/hello.txt"
  28. resp, err := c.Object.Get(context.Background(), name, nil)
  29. if err != nil {
  30. panic(err)
  31. }
  32. bs, _ := ioutil.ReadAll(resp.Body)
  33. resp.Body.Close()
  34. fmt.Printf("%s\n", string(bs))
  35. // range
  36. opt := &cos.ObjectGetOptions{
  37. ResponseContentType: "text/html",
  38. Range: "bytes=0-3",
  39. }
  40. resp, err = c.Object.Get(context.Background(), name, opt)
  41. if err != nil {
  42. panic(err)
  43. }
  44. bs, _ = ioutil.ReadAll(resp.Body)
  45. resp.Body.Close()
  46. fmt.Printf("%s\n", string(bs))
  47. }