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.

66 lines
1.5 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-1259654469.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. // Notice when put a large file and set need the request body, might happend out of memory error.
  22. RequestBody: false,
  23. ResponseHeader: true,
  24. ResponseBody: false,
  25. },
  26. },
  27. })
  28. opt := &cos.ObjectSelectOptions{
  29. Expression: "Select * from COSObject",
  30. ExpressionType: "SQL",
  31. InputSerialization: &cos.SelectInputSerialization{
  32. JSON: &cos.JSONInputSerialization{
  33. Type: "DOCUMENT",
  34. },
  35. },
  36. OutputSerialization: &cos.SelectOutputSerialization{
  37. JSON: &cos.JSONOutputSerialization{
  38. RecordDelimiter: "\n",
  39. },
  40. },
  41. RequestProgress: "TRUE",
  42. }
  43. res, err := c.Object.Select(context.Background(), "test.json", opt)
  44. if err != nil {
  45. panic(err)
  46. }
  47. defer res.Close()
  48. data, err := ioutil.ReadAll(res)
  49. if err != nil {
  50. panic(err)
  51. }
  52. fmt.Printf("data: %v\n", string(data))
  53. resp, _ := res.(*cos.ObjectSelectResponse)
  54. fmt.Printf("data: %+v\n", resp.Frame)
  55. // Select to File
  56. _, err = c.Object.SelectToFile(context.Background(), "test.json", "./test.json", opt)
  57. if err != nil {
  58. panic(err)
  59. }
  60. }