minio.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. "property-company-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 GetObjName(objUrl string) (string) {
  128. array := strings.Split(objUrl, "://")
  129. if len(array) < 2 {
  130. return ""
  131. }
  132. array = strings.Split(array[1], "/")
  133. if len(array) != 3 {
  134. return ""
  135. }
  136. bucketName := array[1]
  137. objName := array[2]
  138. objName = strings.ReplaceAll(objName, "%23%23%23%23", "####")
  139. return bucketName+"-"+objName
  140. }
  141. func ObjExist(endpoint, bucketName, objName string) (bool, error) {
  142. if endpoint == "" {
  143. return false, nil
  144. }
  145. //endpoint := parser.Conf.Oss.Endpoint
  146. accessKeyID := parser.Conf.Oss.Id
  147. secretAccessKey := parser.Conf.Oss.Key
  148. useSSL := false
  149. // Initialize minio client object.
  150. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  151. if err != nil {
  152. return false, err
  153. }
  154. // Make a new bucket called mymusic.
  155. //bucketName := parser.Conf.Oss.PropertyCompanyBucket
  156. //objName := ""
  157. obj, err := minioClient.GetObject(bucketName, objName, minio.GetObjectOptions{})
  158. if err != nil {
  159. return false, err
  160. }
  161. defer obj.Close()
  162. _, err = obj.Stat()
  163. if err != nil {
  164. if strings.Contains(err.Error(), " not exist") {
  165. return false, nil
  166. }
  167. return false, err
  168. }
  169. return true, nil
  170. }
  171. func RemoveFromMinio(objUrl string) (err error) {
  172. _, endpoint, bucketName, objName := parseObjUrl(objUrl)
  173. if endpoint == "" {
  174. return nil
  175. }
  176. //endpoint := parser.Conf.Oss.Endpoint
  177. accessKeyID := parser.Conf.Oss.Id
  178. secretAccessKey := parser.Conf.Oss.Key
  179. useSSL := false
  180. // Initialize minio client object.
  181. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  182. if err != nil {
  183. return err
  184. }
  185. // Make a new bucket called mymusic.
  186. //bucketName := parser.Conf.Oss.PropertyCompanyBucket
  187. //objName := ""
  188. err = minioClient.RemoveObject(bucketName, objName)
  189. if err != nil {
  190. return err
  191. }
  192. return nil
  193. }
  194. func GetFilePath(objName string) (string, error) {
  195. endpoint := parser.Conf.Oss.Endpoint
  196. accessKeyID := parser.Conf.Oss.Id
  197. secretAccessKey := parser.Conf.Oss.Key
  198. useSSL := false
  199. // Initialize minio client object.
  200. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  201. if err != nil {
  202. return "", err
  203. }
  204. // Make a new bucket called mymusic.
  205. bucketName := parser.Conf.Oss.PropertyCompanyBucket
  206. rr, er := minioClient.PresignedGetObject(bucketName, objName, 24*time.Hour, hurl.Values{})
  207. if er != nil {
  208. fmt.Printf("获取文件路径失败:%v\n", er)
  209. return "", er
  210. }
  211. return rr.String(), nil
  212. }
  213. func MiniTest() {
  214. endpoint := "47.108.135.38:9000"
  215. accessKeyID := "minioadmin"
  216. secretAccessKey := "hly@1353406"
  217. useSSL := false
  218. // Initialize minio client object.
  219. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  220. if err != nil {
  221. log.Fatalln(err)
  222. }
  223. // Make a new bucket called mymusic.
  224. bucketName := "testb"
  225. location := ""
  226. err = minioClient.MakeBucket(bucketName, location)
  227. if err != nil {
  228. // Check to see if we already own this bucket (which happens if you run this twice)
  229. exists, errBucketExists := minioClient.BucketExists(bucketName)
  230. if errBucketExists == nil && exists {
  231. log.Printf("We already own %s\n", bucketName)
  232. } else {
  233. log.Fatalln(err)
  234. }
  235. } else {
  236. log.Printf("Successfully created %s\n", bucketName)
  237. }
  238. // Upload the zip file
  239. objectName := "5.png"
  240. filePath := "D:\\5.png"
  241. contentType := ""
  242. // Upload the zip file with FPutObject
  243. n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType})
  244. if err != nil {
  245. log.Fatalln(err)
  246. }
  247. log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
  248. rr, er := minioClient.PresignedGetObject("testb", objectName, 24 * 365 * 100 *time.Hour, hurl.Values{})
  249. if er != nil {
  250. fmt.Printf("xxxx:%v\n", er)
  251. return
  252. }
  253. fmt.Printf("xxxx:%s\n", rr.String())
  254. }