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.

53 lines
1.0 KiB

  1. package main
  2. import (
  3. "context"
  4. "net/url"
  5. "os"
  6. "net/http"
  7. "fmt"
  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_Key"),
  17. SecretKey: os.Getenv("COS_Secret"),
  18. Transport: &debug.DebugRequestTransport{
  19. RequestHeader: true,
  20. RequestBody: false,
  21. ResponseHeader: true,
  22. ResponseBody: true,
  23. },
  24. },
  25. })
  26. name := "test/uploadFile.go"
  27. f, err := os.Open(os.Args[0])
  28. if err != nil {
  29. panic(err)
  30. }
  31. s, err := f.Stat()
  32. if err != nil {
  33. panic(err)
  34. }
  35. fmt.Println(s.Size())
  36. opt := &cos.ObjectPutOptions{
  37. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  38. ContentLength: int(s.Size()),
  39. },
  40. }
  41. //opt.ContentLength = int(s.Size())
  42. _, err = c.Object.Put(context.Background(), name, f, opt)
  43. if err != nil {
  44. panic(err)
  45. }
  46. }