oss.go 4.2 KB

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