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. "fmt"
  9. "io/ioutil"
  10. "time"
  11. "github.com/tencentyun/cos-go-sdk-v5"
  12. "github.com/tencentyun/cos-go-sdk-v5/debug"
  13. )
  14. func main() {
  15. u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
  16. b := &cos.BaseURL{BucketURL: u}
  17. c := cos.NewClient(b, &http.Client{
  18. Transport: &cos.AuthorizationTransport{
  19. SecretID: os.Getenv("COS_SECRETID"),
  20. SecretKey: os.Getenv("COS_SECRETKEY"),
  21. Transport: &debug.DebugRequestTransport{
  22. RequestHeader: true,
  23. RequestBody: true,
  24. ResponseHeader: true,
  25. ResponseBody: true,
  26. },
  27. },
  28. })
  29. source := "test/objectMove1.go"
  30. expected := "test"
  31. f := strings.NewReader(expected)
  32. _, err := c.Object.Put(context.Background(), source, f, nil)
  33. if err != nil {
  34. panic(err)
  35. }
  36. soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
  37. dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond())
  38. //opt := &cos.ObjectCopyOptions{}
  39. res, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
  40. if err != nil {
  41. panic(err)
  42. }
  43. fmt.Printf("%+v\n\n", res)
  44. resp, err := c.Object.Get(context.Background(), dest, nil)
  45. if err != nil {
  46. panic(err)
  47. }
  48. bs, _ := ioutil.ReadAll(resp.Body)
  49. resp.Body.Close()
  50. result := string(bs)
  51. if result != expected {
  52. panic(fmt.Sprintf("%s != %s", result, expected))
  53. }
  54. }