utils.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "log"
  7. "path"
  8. "time"
  9. "github.com/qor/oss"
  10. )
  11. func Upload(storage oss.StorageInterface, path string, reader io.Reader) (err error) {
  12. timeBegin := time.Now()
  13. defer func() {
  14. timeFinish := time.Now()
  15. if err != nil {
  16. //todo error log
  17. log.Println(err)
  18. } else {
  19. log.Printf("upload: %s, time_spent_ms: %s \n", path, fmt.Sprintf("%f", float64(timeFinish.Sub(timeBegin))/float64(time.Millisecond)))
  20. }
  21. }()
  22. _, err = storage.Put(path, reader)
  23. if err != nil {
  24. err = errors.New(fmt.Sprintf("upload error: %v, path: %v", err, path))
  25. return
  26. }
  27. return
  28. }
  29. func DeleteObjects(storage oss.StorageInterface, paths []string) (err error) {
  30. timeBegin := time.Now()
  31. defer func() {
  32. timeFinish := time.Now()
  33. if err != nil {
  34. //todo error log
  35. log.Println(err)
  36. } else {
  37. log.Printf("delete: %s, time_spent_ms: %s \n", paths, fmt.Sprintf("%f", float64(timeFinish.Sub(timeBegin))/float64(time.Millisecond)))
  38. }
  39. }()
  40. if storage, ok := storage.(DeleteObjectsInterface); ok {
  41. var length = len(paths)
  42. var i = 0
  43. for i < length {
  44. var left, right int
  45. left = i
  46. if i+1000 < length {
  47. right = i + 1000
  48. } else {
  49. right = length
  50. }
  51. i = right
  52. err = storage.DeleteObjects(paths[left:right])
  53. if err != nil {
  54. err = errors.New(fmt.Sprintf("delete error: %v, path: %v", err, paths[left:right]))
  55. return
  56. }
  57. }
  58. return
  59. }
  60. for _, v := range paths {
  61. err = storage.Delete(v)
  62. if err != nil {
  63. err = errors.New(fmt.Sprintf("delete error: %v, path: %v", err, v))
  64. return
  65. }
  66. }
  67. return
  68. }
  69. func Copy(storage oss.StorageInterface, from, to string) (err error) {
  70. timeBegin := time.Now()
  71. defer func() {
  72. timeFinish := time.Now()
  73. if err != nil {
  74. //todo error log
  75. log.Println(err)
  76. } else {
  77. log.Printf("copy: from %s to %s, time_spent_ms: %s \n", from, to, fmt.Sprintf("%f", float64(timeFinish.Sub(timeBegin))/float64(time.Millisecond)))
  78. }
  79. }()
  80. if storage, ok := storage.(GetBucketInterface); ok {
  81. from = path.Join(storage.GetBucket(), from)
  82. }
  83. err = storage.(CopyInterface).Copy(from, to)
  84. if err != nil {
  85. err = errors.New(fmt.Sprintf("copy error: %v, from: %v, to: %v", err, from, to))
  86. }
  87. return
  88. }