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. "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. RequestBody: true,
  21. ResponseHeader: true,
  22. ResponseBody: true,
  23. },
  24. },
  25. })
  26. // Case1 normal put object
  27. name := "test/objectPut.go"
  28. f := strings.NewReader("test")
  29. _, err := c.Object.Put(context.Background(), name, f, nil)
  30. if err != nil {
  31. panic(err)
  32. }
  33. // Case2 put object with the options
  34. name = "test/put_option.go"
  35. f = strings.NewReader("test xxx")
  36. opt := &cos.ObjectPutOptions{
  37. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  38. ContentType: "text/html",
  39. },
  40. ACLHeaderOptions: &cos.ACLHeaderOptions{
  41. //XCosACL: "public-read",
  42. XCosACL: "private",
  43. },
  44. }
  45. _, err = c.Object.Put(context.Background(), name, f, opt)
  46. if err != nil {
  47. panic(err)
  48. }
  49. // Case3 put object by local file
  50. _, err = c.Object.PutFromFile(context.Background(), name, "./test10M", nil)
  51. if err != nil {
  52. panic(err)
  53. }
  54. }