oss.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package oss
  2. import (
  3. ossDomain "erpims/application/domain/oss"
  4. "erpims/config"
  5. "erpims/utils"
  6. "errors"
  7. "git.sxidc.com/service-supports/fslog"
  8. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  9. "io"
  10. "strings"
  11. )
  12. var bkt *oss.Bucket
  13. var pfx string
  14. func Init(endpoint, keyID, keySecret, prefix string) error {
  15. pfx = prefix
  16. client, err := oss.New(endpoint, keyID, keySecret)
  17. if err != nil {
  18. return err
  19. }
  20. bkt, err = client.Bucket("fs-yf")
  21. if err != nil {
  22. return err
  23. }
  24. return nil
  25. }
  26. func Touch(reader io.Reader, to string) error {
  27. if reader == nil || utils.HasBlank(to) {
  28. fslog.Warn("oss上传发现违规参数")
  29. return nil
  30. }
  31. return bkt.PutObject(pfx+to, reader)
  32. }
  33. func Url(obj string) (string, error) {
  34. if utils.HasBlank(obj) {
  35. fslog.Warn("oss获取Url发现违规参数")
  36. return "", nil
  37. }
  38. return bkt.SignURL(pfx+obj, oss.HTTPGet, 60)
  39. }
  40. func UrlWithoutPrefix(obj string) (string, error) {
  41. if utils.HasBlank(obj) {
  42. fslog.Warn("oss获取Url发现违规参数")
  43. return "", nil
  44. }
  45. return bkt.SignURL(obj, oss.HTTPGet, 60)
  46. }
  47. func ZoomUrl(obj, s string) (string, error) {
  48. if utils.HasBlank(obj) {
  49. fslog.Warn("oss发现违规参数")
  50. return "", nil
  51. }
  52. return bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s))
  53. }
  54. func Exist(obj string) (bool, error) {
  55. if utils.HasBlank(obj) {
  56. return false, nil
  57. }
  58. return bkt.IsObjectExist(pfx + obj)
  59. }
  60. func ExistWithoutPrefix(obj string) (bool, error) {
  61. if utils.HasBlank(obj) {
  62. return false, nil
  63. }
  64. return bkt.IsObjectExist(obj)
  65. }
  66. func Rm(objs ...string) error {
  67. L := len(objs)
  68. if L == 0 {
  69. return nil
  70. }
  71. result, err := bkt.DeleteObjects(objs)
  72. if err != nil {
  73. return err
  74. }
  75. if len(result.DeletedObjects) != L {
  76. go func() {
  77. // 日志打印哪些Obj删除失败
  78. sucMap := make(map[string]struct{}, len(result.DeletedObjects))
  79. for _, deleted := range result.DeletedObjects {
  80. sucMap[deleted] = struct{}{}
  81. }
  82. for _, obj := range objs {
  83. if _, ok := sucMap[obj]; !ok {
  84. fslog.Warn("Oss删除%s图片失败")
  85. }
  86. }
  87. }()
  88. }
  89. return nil
  90. }
  91. func Ls(dir string) ([]string, error) {
  92. if utils.HasBlank(dir) {
  93. return []string{}, nil
  94. }
  95. v2, err := bkt.ListObjectsV2(oss.Prefix(pfx + dir))
  96. if err != nil {
  97. return nil, err
  98. }
  99. ks := make([]string, len(v2.Objects))
  100. for i, obj := range v2.Objects {
  101. ks[i] = obj.Key
  102. }
  103. return ks, nil
  104. }
  105. func Mv(f, t string) error {
  106. if err := Cp(f, t); err != nil {
  107. return err
  108. }
  109. return bkt.DeleteObject(pfx + f)
  110. }
  111. func Cp(f, t string) error {
  112. _, err := bkt.CopyObject(pfx+f, pfx+t)
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func MvWithoutPrefix(f, t string) error {
  119. if _, err := bkt.CopyObject(f, t); err != nil {
  120. return err
  121. }
  122. return bkt.DeleteObject(f)
  123. }
  124. func MultipleFileMv(filePath string) (string, error) {
  125. endPicture := make([]string, 0)
  126. files := strings.Split(filePath, ",")
  127. for _, file := range files {
  128. //文件名
  129. tmpFx := config.GetConfig().OssConfig.TmpPrefix
  130. if utils.HasText(file) && strings.Contains(file, "/temp/") {
  131. fileName, found := strings.CutPrefix(file, tmpFx)
  132. if !found {
  133. return "", errors.New("文件名处理失败")
  134. }
  135. err := MvWithoutPrefix(file, pfx+fileName)
  136. if err != nil {
  137. return "", errors.New("文件移动失败")
  138. }
  139. endPicture = append(endPicture, pfx+fileName)
  140. continue
  141. }
  142. endPicture = append(endPicture, file)
  143. }
  144. return strings.Join(endPicture, ","), nil
  145. }
  146. func MultipleFileUrl(filePath string) ([]ossDomain.FileObject, error) {
  147. filePathArr := strings.Split(filePath, ",")
  148. picture := make([]ossDomain.FileObject, 0)
  149. for _, file := range filePathArr {
  150. exist, err := ExistWithoutPrefix(file)
  151. if err != nil {
  152. fslog.Error(err)
  153. picture = append(picture, ossDomain.FileObject{
  154. Url: file,
  155. Temp: "",
  156. })
  157. continue
  158. }
  159. if !exist {
  160. fslog.Error("文件【" + file + "】不存在")
  161. picture = append(picture, ossDomain.FileObject{
  162. Url: file,
  163. Temp: "",
  164. })
  165. continue
  166. }
  167. url, err := UrlWithoutPrefix(file)
  168. if err != nil {
  169. fslog.Error("文件【" + file + "】链接生成失败")
  170. picture = append(picture, ossDomain.FileObject{
  171. Url: file,
  172. Temp: "",
  173. })
  174. continue
  175. }
  176. name, _ := strings.CutPrefix(file, pfx+"/")
  177. picture = append(picture, ossDomain.FileObject{
  178. Url: file,
  179. Name: name,
  180. Temp: url,
  181. })
  182. }
  183. return picture, nil
  184. }