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.

64 lines
1.4 KiB

  1. package main
  2. import (
  3. "context"
  4. "net/url"
  5. "os"
  6. "strings"
  7. "net/http"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  9. "github.com/tencentyun/cos-go-sdk-v5/debug"
  10. )
  11. func main() {
  12. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  13. b := &cos.BaseURL{BucketURL: u}
  14. c := cos.NewClient(b, &http.Client{
  15. Transport: &cos.AuthorizationTransport{
  16. SecretID: os.Getenv("COS_SECRETID"),
  17. SecretKey: os.Getenv("COS_SECRETKEY"),
  18. Transport: &debug.DebugRequestTransport{
  19. RequestHeader: true,
  20. // Notice when put a large file and set need the request body, might happend out of memory error.
  21. RequestBody: false,
  22. ResponseHeader: true,
  23. ResponseBody: true,
  24. },
  25. },
  26. })
  27. // Case1 normal put object
  28. name := "test/objectPut.go"
  29. f := strings.NewReader("test")
  30. _, err := c.Object.Put(context.Background(), name, f, nil)
  31. if err != nil {
  32. panic(err)
  33. }
  34. // Case2 put object with the options
  35. name = "test/put_option.go"
  36. f = strings.NewReader("test xxx")
  37. opt := &cos.ObjectPutOptions{
  38. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  39. ContentType: "text/html",
  40. },
  41. ACLHeaderOptions: &cos.ACLHeaderOptions{
  42. //XCosACL: "public-read",
  43. XCosACL: "private",
  44. },
  45. }
  46. _, err = c.Object.Put(context.Background(), name, f, opt)
  47. if err != nil {
  48. panic(err)
  49. }
  50. // Case3 put object by local file path
  51. _, err = c.Object.PutFromFile(context.Background(), name, "./test", nil)
  52. if err != nil {
  53. panic(err)
  54. }
  55. }