minio.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/cache"
  6. "github.com/minio/minio-go/v6"
  7. "io"
  8. "log"
  9. "math/rand"
  10. hurl "net/url"
  11. "xingjia-management-gateway/parser"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. NUmStr = "0123456789"
  17. CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  18. SpecStr = "+=-@#~,.[]()!%^*$"
  19. )
  20. func GenerateRandomStr(length int, charset string) string {
  21. time.Sleep(1*time.Microsecond)
  22. rand.Seed(time.Now().UnixNano())
  23. //初始化密码切片
  24. var passwd []byte = make([]byte, length, length)
  25. //源字符串
  26. var sourceStr string
  27. //判断字符类型,如果是数字
  28. if charset == "num" {
  29. sourceStr = NUmStr
  30. //如果选的是字符
  31. } else if charset == "char" {
  32. sourceStr = charset
  33. //如果选的是混合模式
  34. } else if charset == "mix" {
  35. sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr)
  36. //如果选的是高级模式
  37. } else if charset == "advance" {
  38. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  39. } else {
  40. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  41. }
  42. //遍历,生成一个随机index索引,
  43. for i := 0; i < length; i++ {
  44. index := rand.Intn(len(sourceStr))
  45. passwd[i] = sourceStr[index]
  46. }
  47. return string(passwd)
  48. }
  49. func UploadToMinio(fileName string, r io.Reader, size int64, imgMine string) (objName string, err error) {
  50. endpoint := parser.Conf.Oss.Endpoint
  51. accessKeyID := parser.Conf.Oss.Id
  52. secretAccessKey := parser.Conf.Oss.Key
  53. useSSL := false
  54. // Initialize minio client object.
  55. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  56. if err != nil {
  57. return "", err
  58. }
  59. // Make a new bucket called mymusic.
  60. bucketName := parser.Conf.Oss.PropertyCompanyBucket
  61. contentType := imgMine
  62. objectName := ""
  63. count := 0
  64. exist := false
  65. array := strings.Split(fileName, ".")
  66. tail := ""
  67. if len(array) > 0 {
  68. tail = array[len(array)-1]
  69. }
  70. rkeyPrefix := "oss_obj_exist_"
  71. rkey := ""
  72. defer func() {
  73. if rkey != "" {
  74. cache.RedisUnlock(rkey)
  75. }
  76. }()
  77. for ;count < 10;count++ {
  78. if rkey != "" {
  79. cache.RedisUnlock(rkey)
  80. rkey = ""
  81. }
  82. objectName = fmt.Sprintf("%d####", time.Now().Unix()) + GenerateRandomStr(32, "mix") +"."+tail
  83. // 互斥判断文件是否存在
  84. rkey = rkeyPrefix + objectName
  85. if !cache.RedisLock(rkey) {
  86. time.Sleep(200*time.Millisecond)
  87. rkey = ""
  88. continue
  89. }
  90. exist, err = ObjExist(endpoint, bucketName, objectName)
  91. if err != nil {
  92. return "", err
  93. }
  94. if exist {
  95. continue
  96. }
  97. // Upload the zip file with FPutObject
  98. _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType:contentType})
  99. if err != nil {
  100. return "", err
  101. }
  102. ret := parser.Conf.Oss.Protocol +"://"+endpoint+"/"+bucketName+"/"+hurl.QueryEscape(objectName)
  103. //ObjTaskAdd(ret)
  104. return ret, nil
  105. }
  106. if exist {
  107. return "", errors.New("文件已存在")
  108. }
  109. return "", errors.New("系统繁忙")
  110. }
  111. func parseObjUrl(objUrl string) (string, string, string, string) {
  112. array := strings.Split(objUrl, "://")
  113. protocol, endpoint, bucketName, objName := "", "", "", ""
  114. if len(array) < 2 {
  115. return "", "", "", ""
  116. }
  117. protocol = array[0]
  118. array = strings.Split(array[1], "/")
  119. if len(array) != 3 {
  120. return "", "", "", ""
  121. }
  122. endpoint = array[0]
  123. bucketName = array[1]
  124. objName = array[2]
  125. return protocol, endpoint, bucketName, objName
  126. }
  127. func ObjExist(endpoint, bucketName, objName string) (bool, error) {
  128. if endpoint == "" {
  129. return false, nil
  130. }
  131. //endpoint := parser.Conf.Oss.Endpoint
  132. accessKeyID := parser.Conf.Oss.Id
  133. secretAccessKey := parser.Conf.Oss.Key
  134. useSSL := false
  135. // Initialize minio client object.
  136. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  137. if err != nil {
  138. return false, err
  139. }
  140. // Make a new bucket called mymusic.
  141. //bucketName := parser.Conf.Oss.PropertyCompanyBucket
  142. //objName := ""
  143. obj, err := minioClient.GetObject(bucketName, objName, minio.GetObjectOptions{})
  144. if err != nil {
  145. return false, err
  146. }
  147. defer obj.Close()
  148. _, err = obj.Stat()
  149. if err != nil {
  150. if strings.Contains(err.Error(), " not exist") {
  151. return false, nil
  152. }
  153. return false, err
  154. }
  155. return true, nil
  156. }
  157. func RemoveFromMinio(objUrl string) (err error) {
  158. _, endpoint, bucketName, objName := parseObjUrl(objUrl)
  159. if endpoint == "" {
  160. return nil
  161. }
  162. //endpoint := parser.Conf.Oss.Endpoint
  163. accessKeyID := parser.Conf.Oss.Id
  164. secretAccessKey := parser.Conf.Oss.Key
  165. useSSL := false
  166. // Initialize minio client object.
  167. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  168. if err != nil {
  169. return err
  170. }
  171. // Make a new bucket called mymusic.
  172. //bucketName := parser.Conf.Oss.PropertyCompanyBucket
  173. //objName := ""
  174. err = minioClient.RemoveObject(bucketName, objName)
  175. if err != nil {
  176. return err
  177. }
  178. return nil
  179. }
  180. func GetFilePath(objName string) (string, error) {
  181. endpoint := parser.Conf.Oss.Endpoint
  182. accessKeyID := parser.Conf.Oss.Id
  183. secretAccessKey := parser.Conf.Oss.Key
  184. useSSL := false
  185. // Initialize minio client object.
  186. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  187. if err != nil {
  188. return "", err
  189. }
  190. // Make a new bucket called mymusic.
  191. bucketName := parser.Conf.Oss.PropertyCompanyBucket
  192. rr, er := minioClient.PresignedGetObject(bucketName, objName, 24*time.Hour, hurl.Values{})
  193. if er != nil {
  194. fmt.Printf("获取文件路径失败:%v\n", er)
  195. return "", er
  196. }
  197. return rr.String(), nil
  198. }
  199. func MiniTest() {
  200. endpoint := "47.108.135.38:9000"
  201. accessKeyID := "minioadmin"
  202. secretAccessKey := "hly@1353406"
  203. useSSL := false
  204. // Initialize minio client object.
  205. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  206. if err != nil {
  207. log.Fatalln(err)
  208. }
  209. // Make a new bucket called mymusic.
  210. bucketName := "testb"
  211. location := ""
  212. err = minioClient.MakeBucket(bucketName, location)
  213. if err != nil {
  214. // Check to see if we already own this bucket (which happens if you run this twice)
  215. exists, errBucketExists := minioClient.BucketExists(bucketName)
  216. if errBucketExists == nil && exists {
  217. log.Printf("We already own %s\n", bucketName)
  218. } else {
  219. log.Fatalln(err)
  220. }
  221. } else {
  222. log.Printf("Successfully created %s\n", bucketName)
  223. }
  224. // Upload the zip file
  225. objectName := "5.png"
  226. filePath := "D:\\5.png"
  227. contentType := ""
  228. // Upload the zip file with FPutObject
  229. n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType})
  230. if err != nil {
  231. log.Fatalln(err)
  232. }
  233. log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
  234. rr, er := minioClient.PresignedGetObject("testb", objectName, 24 * 365 * 100 *time.Hour, hurl.Values{})
  235. if er != nil {
  236. fmt.Printf("xxxx:%v\n", er)
  237. return
  238. }
  239. fmt.Printf("xxxx:%s\n", rr.String())
  240. }