oss.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package oss
  2. import (
  3. "bytes"
  4. "errors"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. "io"
  8. "log"
  9. )
  10. var Bkt *oss.Bucket
  11. var pfx string
  12. func Init(endpoint, keyID, keySecret, prefix string) error {
  13. pfx = prefix
  14. client, err := oss.New(endpoint, keyID, keySecret)
  15. if err != nil {
  16. return err
  17. }
  18. Bkt, err = client.Bucket("fs-yf")
  19. if err != nil {
  20. return err
  21. }
  22. return nil
  23. }
  24. func Touch(reader io.Reader, to string) error {
  25. return TouchPrefix(reader, to, true)
  26. }
  27. func TouchPrefix(reader io.Reader, to string, addPfx bool) error {
  28. if addPfx {
  29. return Bkt.PutObject(pfx+to, reader)
  30. }
  31. return Bkt.PutObject(to, reader)
  32. }
  33. func Url(obj string) (string, error) {
  34. return UrlPfx(obj, true)
  35. }
  36. func UrlPfx(obj string, addPfx bool) (string, error) {
  37. if strutils.HasBlank(obj) {
  38. log.Println("oss获取Url发现违规参数")
  39. return "", nil
  40. }
  41. if addPfx {
  42. return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60*60*60*365)
  43. }
  44. return Bkt.SignURL(obj, oss.HTTPGet, 60*60*60*365)
  45. }
  46. func ZoomUrl(obj, s string) (string, error) {
  47. if strutils.HasBlank(obj) {
  48. log.Println("oss发现违规参数")
  49. return "", nil
  50. }
  51. return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s))
  52. }
  53. func ZoomUrlPfx(obj, s string, addPfx bool) (string, error) {
  54. if strutils.HasBlank(obj) {
  55. log.Println("oss发现违规参数")
  56. return "", nil
  57. }
  58. if addPfx {
  59. return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s))
  60. }
  61. return Bkt.SignURL(obj, oss.HTTPGet, 60, oss.Process(s))
  62. }
  63. func Exist(obj string) (bool, error) {
  64. if strutils.HasBlank(obj) {
  65. return false, nil
  66. }
  67. return Bkt.IsObjectExist(pfx + obj)
  68. }
  69. func Rm(objs ...string) error {
  70. L := len(objs)
  71. if L == 0 {
  72. return nil
  73. }
  74. result, err := Bkt.DeleteObjects(objs)
  75. if err != nil {
  76. return err
  77. }
  78. if len(result.DeletedObjects) != L {
  79. go func() {
  80. // 日志打印哪些Obj删除失败
  81. sucMap := make(map[string]struct{}, len(result.DeletedObjects))
  82. for _, deleted := range result.DeletedObjects {
  83. sucMap[deleted] = struct{}{}
  84. }
  85. for _, obj := range objs {
  86. if _, ok := sucMap[obj]; !ok {
  87. log.Println("Oss删除%s图片失败")
  88. }
  89. }
  90. }()
  91. }
  92. return nil
  93. }
  94. func Ls(dir string) ([]string, error) {
  95. if strutils.HasBlank(dir) {
  96. return []string{}, nil
  97. }
  98. v2, err := Bkt.ListObjectsV2(oss.Prefix(pfx + dir))
  99. if err != nil {
  100. return nil, err
  101. }
  102. ks := make([]string, len(v2.Objects))
  103. for i, obj := range v2.Objects {
  104. ks[i] = obj.Key
  105. }
  106. return ks, nil
  107. }
  108. func Mv(f, t string) error {
  109. if err := Cp(f, t); err != nil {
  110. return err
  111. }
  112. return Bkt.DeleteObject(pfx + f)
  113. }
  114. func CpOrigin(f, t string) error {
  115. _, err := Bkt.CopyObject(f, t)
  116. return err
  117. }
  118. func Cp(f, t string) error {
  119. return CpOrigin(pfx+f, pfx+t)
  120. }
  121. func TouchFormatImg(reader io.Reader, to string) error {
  122. if reader == nil || strutils.HasBlank(to) {
  123. log.Println("oss上传发现违规参数")
  124. return nil
  125. }
  126. return Bkt.PutObject(pfx+to, reader, oss.ContentType("image/jpg"))
  127. }
  128. func GetObjectBytes(obj string) ([]byte, error) {
  129. if strutils.HasBlank(obj) {
  130. return nil, errors.New("参数错误")
  131. }
  132. buf := new(bytes.Buffer)
  133. body, err := Bkt.GetObject(pfx + obj)
  134. if err != nil {
  135. return nil, err
  136. }
  137. _, err = io.Copy(buf, body)
  138. if err != nil {
  139. return nil, err
  140. }
  141. err = body.Close()
  142. if err != nil {
  143. return nil, err
  144. }
  145. return buf.Bytes(), nil
  146. }
  147. func MvWithoutPrefix(f, t string) error {
  148. if _, err := Bkt.CopyObject(f, t); err != nil {
  149. return err
  150. }
  151. return Bkt.DeleteObject(f)
  152. }