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.

77 lines
1.7 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 log_status(err error) {
  15. if err == nil {
  16. return
  17. }
  18. if cos.IsNotFoundError(err) {
  19. // WARN
  20. fmt.Println("WARN: Resource is not existed")
  21. } else if e, ok := cos.IsCOSError(err); ok {
  22. fmt.Printf("ERROR: Code: %v\n", e.Code)
  23. fmt.Printf("ERROR: Message: %v\n", e.Message)
  24. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  25. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  26. // ERROR
  27. } else {
  28. fmt.Printf("ERROR: %v\n", err)
  29. // ERROR
  30. }
  31. }
  32. func main() {
  33. u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
  34. b := &cos.BaseURL{BucketURL: u}
  35. c := cos.NewClient(b, &http.Client{
  36. Transport: &cos.AuthorizationTransport{
  37. SecretID: os.Getenv("COS_SECRETID"),
  38. SecretKey: os.Getenv("COS_SECRETKEY"),
  39. Transport: &debug.DebugRequestTransport{
  40. RequestHeader: true,
  41. RequestBody: true,
  42. ResponseHeader: true,
  43. ResponseBody: true,
  44. },
  45. },
  46. })
  47. source := "test/objectMove1.go"
  48. expected := "test"
  49. f := strings.NewReader(expected)
  50. _, err := c.Object.Put(context.Background(), source, f, nil)
  51. log_status(err)
  52. soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
  53. dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond())
  54. //opt := &cos.ObjectCopyOptions{}
  55. res, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
  56. log_status(err)
  57. fmt.Printf("%+v\n\n", res)
  58. resp, err := c.Object.Get(context.Background(), dest, nil)
  59. log_status(err)
  60. bs, _ := ioutil.ReadAll(resp.Body)
  61. resp.Body.Close()
  62. result := string(bs)
  63. if result != expected {
  64. panic(fmt.Sprintf("%s != %s", result, expected))
  65. }
  66. }