oss.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package oss
  2. import (
  3. "bytes"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. "github.com/pkg/errors"
  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. type ObjectInfo struct {
  109. Key string
  110. Type string
  111. Size int64
  112. }
  113. func LsDetails(dir string) ([]ObjectInfo, error) {
  114. if strutils.HasBlank(dir) {
  115. return []ObjectInfo{}, nil
  116. }
  117. v2, err := Bkt.ListObjectsV2(oss.Prefix(pfx + dir))
  118. if err != nil {
  119. return nil, err
  120. }
  121. ks := make([]ObjectInfo, len(v2.Objects))
  122. for i, obj := range v2.Objects {
  123. ks[i] = ObjectInfo{
  124. Key: obj.Key,
  125. Type: obj.Type,
  126. Size: obj.Size,
  127. }
  128. }
  129. return ks, nil
  130. }
  131. func Mv(f, t string) error {
  132. if err := Cp(f, t); err != nil {
  133. return err
  134. }
  135. return Bkt.DeleteObject(pfx + f)
  136. }
  137. func CpOrigin(f, t string) error {
  138. _, err := Bkt.CopyObject(f, t)
  139. return err
  140. }
  141. func Cp(f, t string) error {
  142. return CpOrigin(pfx+f, pfx+t)
  143. }
  144. func TouchFormatImg(reader io.Reader, to string) error {
  145. if reader == nil || strutils.HasBlank(to) {
  146. log.Println("oss上传发现违规参数")
  147. return nil
  148. }
  149. return Bkt.PutObject(pfx+to, reader, oss.ContentType("image/jpg"))
  150. }
  151. func GetObjectBytes(obj string) ([]byte, error) {
  152. if strutils.HasBlank(obj) {
  153. return nil, errors.New("参数错误")
  154. }
  155. buf := new(bytes.Buffer)
  156. body, err := Bkt.GetObject(pfx + obj)
  157. if err != nil {
  158. return nil, err
  159. }
  160. _, err = io.Copy(buf, body)
  161. if err != nil {
  162. return nil, err
  163. }
  164. err = body.Close()
  165. if err != nil {
  166. return nil, err
  167. }
  168. return buf.Bytes(), nil
  169. }
  170. func MvWithoutPrefix(f, t string) error {
  171. if _, err := Bkt.CopyObject(f, t); err != nil {
  172. return err
  173. }
  174. return Bkt.DeleteObject(f)
  175. }