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.

63 lines
1.3 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "time"
  11. "github.com/tencentyun/cos-go-sdk-v5"
  12. "github.com/tencentyun/cos-go-sdk-v5/debug"
  13. )
  14. func main() {
  15. ak := os.Getenv("COS_SECRETID")
  16. sk := os.Getenv("COS_SECRETKEY")
  17. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  18. b := &cos.BaseURL{BucketURL: u}
  19. c := cos.NewClient(b, &http.Client{
  20. Transport: &cos.AuthorizationTransport{
  21. SecretID: ak,
  22. SecretKey: sk,
  23. Expire: time.Hour,
  24. Transport: &debug.DebugRequestTransport{
  25. RequestHeader: true,
  26. RequestBody: true,
  27. ResponseHeader: true,
  28. ResponseBody: true,
  29. },
  30. },
  31. })
  32. name := "test"
  33. ctx := context.Background()
  34. // Normal header way to get object
  35. resp, err := c.Object.Get(ctx, name, nil)
  36. if err != nil {
  37. panic(err)
  38. }
  39. bs, _ := ioutil.ReadAll(resp.Body)
  40. resp.Body.Close()
  41. // Get presigned
  42. presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, ak, sk, time.Hour, nil)
  43. if err != nil {
  44. panic(err)
  45. }
  46. // Get object by presinged url
  47. resp2, err := http.Get(presignedURL.String())
  48. if err != nil {
  49. panic(err)
  50. }
  51. bs2, _ := ioutil.ReadAll(resp2.Body)
  52. resp2.Body.Close()
  53. fmt.Printf("result2 is : %s\n", string(bs2))
  54. fmt.Printf("%v\n\n", bytes.Compare(bs2, bs) == 0)
  55. }