minio.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/minio/minio-go/v7"
  8. "github.com/minio/minio-go/v7/pkg/credentials"
  9. "log"
  10. "time"
  11. )
  12. //func GetMinIOSTSToken() (item *Token, err error) {
  13. // // MinIO服务的访问信息
  14. // item = new(Token)
  15. // //useSSL := false
  16. // //if utils.MinIoUseSSL == "true" {
  17. // // useSSL = true
  18. // //}
  19. // // 创建MinIO客户端
  20. // //minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
  21. // // Creds: credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
  22. // // Secure: useSSL,
  23. // //})
  24. // //if err != nil {
  25. // // return nil, err
  26. // //}
  27. // // 设置STS凭证请求参数
  28. // //policy := `{
  29. // // "Version": "2012-10-17",
  30. // // "Statement": [
  31. // // {
  32. // // "Sid": "",
  33. // // "Effect": "Allow",
  34. // // "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
  35. // // "Action": "s3:GetObject",
  36. // // "Resource": "arn:aws:s3:::<YourBucketName>/*"
  37. // // }
  38. // // ]
  39. // //}`
  40. // //expiry := time.Hour * 24 // STS凭证的过期时间
  41. // //获取STS凭证
  42. // //stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
  43. // //if err != nil {
  44. // // return
  45. // //}
  46. // item.AccessKeyId = utils.MinIoAccessKeyId
  47. // item.SecretKeyId = utils.MinIoAccessKeySecret
  48. // item.Endpoint = utils.MinIoEndpoint
  49. // item.ImgHost = utils.MinIoImghost
  50. // item.Bucketname = utils.MinIoBucketname
  51. // item.UseSSL = utils.MinIoUseSSL
  52. // item.RegionId = utils.MinIoRegion
  53. // item.Port = utils.MinIoPort
  54. // return
  55. //}
  56. //type Token struct {
  57. // AccessKeyId string
  58. // SecretKeyId string
  59. // RegionId string
  60. // Bucketname string
  61. // Endpoint string
  62. // ImgHost string
  63. // UseSSL string
  64. // Port string
  65. //}
  66. //func UploadMinIo() {
  67. // ctx := context.Background()
  68. // endpoint := "8.136.199.33:9000/"
  69. // accessKeyID := "LfQ8uiJiLP7vLxjRrmNW"
  70. // secretAccessKey := "IszGVHsNicJMQxHC46cYFtbrOiapo0ynwOIJ6c2R"
  71. // useSSL := false
  72. //
  73. // // Initialize minio client object.
  74. // minioClient, err := minio.New(endpoint, &minio.Options{
  75. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  76. // Secure: useSSL,
  77. // })
  78. // if err != nil {
  79. // log.Fatalln(err)
  80. // }
  81. //
  82. // // Make a new bucket called mymusic.
  83. // bucketName := "etastatic"
  84. // location := "/"
  85. //
  86. // err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
  87. // if err != nil {
  88. // // Check to see if we already own this bucket (which happens if you run this twice)
  89. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  90. // if errBucketExists == nil && exists {
  91. // log.Printf("We already own %s\n", bucketName)
  92. // } else {
  93. // log.Fatalln(err)
  94. // }
  95. // } else {
  96. // log.Printf("Successfully created %s\n", bucketName)
  97. // }
  98. // //buckets, err := minioClient.ListBuckets(ctx)
  99. // //for _, bucket := range buckets {
  100. // // fmt.Println(bucket)
  101. // //}
  102. // // Upload the zip file
  103. // objectName := "1111.xlsx"
  104. // filePath := "/Users/xi/Desktop/1111.xlsx"
  105. // contentType := "application/xlsx"
  106. //
  107. // // Upload the zip file with FPutObject
  108. // info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
  109. // if err != nil {
  110. // log.Fatalln(err)
  111. // }
  112. //
  113. // log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
  114. //}
  115. // UploadImgToMinIo 图片上传
  116. //func UploadImgToMinIo(fileName, filePath string) (string, error) {
  117. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  118. // return "0", errors.New("MinIo信息未配置")
  119. // }
  120. //
  121. // ctx := context.Background()
  122. // endpoint := utils.MinIoEndpoint
  123. // accessKeyID := utils.MinIoAccessKeyId
  124. // secretAccessKey := utils.MinIoAccessKeySecret
  125. // useSSL := false
  126. // if utils.MinIoUseSSL == "true" {
  127. // useSSL = true
  128. // }
  129. // minioClient, err := minio.New(endpoint, &minio.Options{
  130. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  131. // Secure: useSSL,
  132. // })
  133. // if err != nil {
  134. // log.Fatalln(err)
  135. // return "1", err
  136. // }
  137. // bucketName := utils.MinIoBucketname
  138. // // Check to see if we already own this bucket (which happens if you run this twice)
  139. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  140. // if errBucketExists == nil && exists {
  141. // log.Printf("We already own %s\n", bucketName)
  142. // } else {
  143. // log.Fatalln(err)
  144. // return "2", err
  145. // }
  146. // path := utils.MinIoUploadDir + time.Now().Format("200601/20060102/")
  147. // path += fileName
  148. // // Upload the zip file with FPutObject
  149. // //contentType := "application/xlsx"
  150. // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  151. // if err != nil {
  152. // log.Fatalln(err)
  153. // return "3", err
  154. // }
  155. //
  156. // path = utils.MinIoImghost + path
  157. // return path, err
  158. //}
  159. // UploadAudioToMinIo 音频上传
  160. //func UploadAudioToMinIo(fileName, filePath string) (string, error) {
  161. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  162. // return "0", errors.New("MinIo信息未配置")
  163. // }
  164. //
  165. // ctx := context.Background()
  166. // endpoint := utils.MinIoEndpoint
  167. // accessKeyID := utils.MinIoAccessKeyId
  168. // secretAccessKey := utils.MinIoAccessKeySecret
  169. // useSSL := false
  170. // if utils.MinIoUseSSL == "true" {
  171. // useSSL = true
  172. // }
  173. // minioClient, err := minio.New(endpoint, &minio.Options{
  174. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  175. // Secure: useSSL,
  176. // })
  177. // if err != nil {
  178. // log.Fatalln(err)
  179. // return "1", err
  180. // }
  181. // bucketName := utils.MinIoBucketname
  182. // // Check to see if we already own this bucket (which happens if you run this twice)
  183. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  184. // if errBucketExists == nil && exists {
  185. // log.Printf("We already own %s\n", bucketName)
  186. // } else {
  187. // log.Fatalln(err)
  188. // return "2", err
  189. // }
  190. //
  191. // path := utils.MinIoUpload_Audio_Dir + time.Now().Format("200601/20060102/")
  192. // path += fileName
  193. //
  194. // // Upload the zip file with FPutObject
  195. // //contentType := "application/xlsx"
  196. // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  197. // if err != nil {
  198. // log.Fatalln(err)
  199. // return "3", err
  200. // }
  201. //
  202. // path = utils.MinIoImghost + path
  203. // return path, err
  204. //}
  205. // UploadVideoToMinIo 视频上传
  206. //func UploadVideoToMinIo(filename, filePath, savePath string) error {
  207. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  208. // return errors.New("MinIo信息未配置")
  209. // }
  210. // defer func() {
  211. // os.Remove(filePath)
  212. // }()
  213. //
  214. // ctx := context.Background()
  215. // endpoint := utils.MinIoEndpoint
  216. // accessKeyID := utils.MinIoAccessKeyId
  217. // secretAccessKey := utils.MinIoAccessKeySecret
  218. // useSSL := false
  219. // if utils.MinIoUseSSL == "true" {
  220. // useSSL = true
  221. // }
  222. // minioClient, err := minio.New(endpoint, &minio.Options{
  223. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  224. // Secure: useSSL,
  225. // })
  226. // if err != nil {
  227. // log.Fatalln(err)
  228. // return err
  229. // }
  230. // bucketName := utils.MinIoBucketname
  231. // // Check to see if we already own this bucket (which happens if you run this twice)
  232. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  233. // if errBucketExists == nil && exists {
  234. // log.Printf("We already own %s\n", bucketName)
  235. // } else {
  236. // log.Fatalln(err)
  237. // return err
  238. // }
  239. //
  240. // //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  241. // //path += filename
  242. // _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  243. // if err != nil {
  244. // log.Fatalln(err)
  245. // return err
  246. // }
  247. // //path = utils.Imghost + path
  248. // //return path,err
  249. // return err
  250. //}
  251. // UploadFileToMinIo 上传文件
  252. //func UploadFileToMinIo(filename, filePath, savePath string) error {
  253. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  254. // return errors.New("MinIo信息未配置")
  255. // }
  256. // defer func() {
  257. // os.Remove(filePath)
  258. // }()
  259. // ctx := context.Background()
  260. // endpoint := utils.MinIoEndpoint
  261. // accessKeyID := utils.MinIoAccessKeyId
  262. // secretAccessKey := utils.MinIoAccessKeySecret
  263. // useSSL := false
  264. // if utils.MinIoUseSSL == "true" {
  265. // useSSL = true
  266. // }
  267. // minioClient, err := minio.New(endpoint, &minio.Options{
  268. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  269. // Secure: useSSL,
  270. // })
  271. // if err != nil {
  272. // log.Fatalln(err)
  273. // return err
  274. // }
  275. // bucketName := utils.MinIoBucketname
  276. // // Check to see if we already own this bucket (which happens if you run this twice)
  277. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  278. // if errBucketExists == nil && exists {
  279. // log.Printf("We already own %s\n", bucketName)
  280. // } else {
  281. // log.Fatalln(err)
  282. // return err
  283. // }
  284. // //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  285. // //path += filename
  286. // _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  287. // if err != nil {
  288. // log.Fatalln(err)
  289. // return err
  290. // }
  291. // //path = utils.Imghost + path
  292. // //return path,err
  293. // return err
  294. //}
  295. // UploadMinIoToDir 上传至hzchart
  296. //func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
  297. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  298. // return "0", errors.New("MinIo信息未配置")
  299. // }
  300. // ctx := context.Background()
  301. // endpoint := utils.MinIoEndpoint
  302. // accessKeyID := utils.MinIoAccessKeyId
  303. // secretAccessKey := utils.MinIoAccessKeySecret
  304. // useSSL := false
  305. // if utils.MinIoUseSSL == "true" {
  306. // useSSL = true
  307. // }
  308. // minioClient, err := minio.New(endpoint, &minio.Options{
  309. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  310. // Secure: useSSL,
  311. // })
  312. // if err != nil {
  313. // log.Fatalln(err)
  314. // return "1", err
  315. // }
  316. // bucketName := utils.MinIoBucketname
  317. // // Check to see if we already own this bucket (which happens if you run this twice)
  318. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  319. // if errBucketExists == nil && exists {
  320. // log.Printf("We already own %s\n", bucketName)
  321. // } else {
  322. // log.Fatalln(err)
  323. // return "2", err
  324. // }
  325. // if uploadDir == "" {
  326. // uploadDir = utils.MinIoUploadDir
  327. // }
  328. // if fileDir == "" {
  329. // fileDir = time.Now().Format("200601/20060102/")
  330. // }
  331. // path := uploadDir + fileDir
  332. // path += filename
  333. // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  334. // if err != nil {
  335. // log.Fatalln(err)
  336. // return "3", err
  337. // }
  338. // path = utils.MinIoImghost + path
  339. // return path, err
  340. //}
  341. //func UploadImgToMinIoTest(fileName, filePath string) (string, error) {
  342. // ctx := context.Background()
  343. // endpoint := utils.Endpoint
  344. // accessKeyID := utils.AccessKeyId
  345. // secretAccessKey := utils.AccessKeySecret
  346. // useSSL := false
  347. // if utils.MinIoUseSSL == "true" {
  348. // useSSL = true
  349. // }
  350. // minioClient, err := minio.New(endpoint, &minio.Options{
  351. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  352. // Secure: useSSL,
  353. // })
  354. // if err != nil {
  355. // log.Fatalln(err)
  356. // return "1", err
  357. // }
  358. // bucketName := utils.Bucketname
  359. // // Check to see if we already own this bucket (which happens if you run this twice)
  360. //
  361. // buckets, e := minioClient.ListBuckets(ctx)
  362. // if e != nil {
  363. // fmt.Println("ListBuckets: ", e.Error())
  364. // return "", e
  365. // }
  366. // for k := range buckets {
  367. // fmt.Println(k)
  368. // }
  369. //
  370. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  371. // fmt.Println("exists: ", exists)
  372. // fmt.Println("errBucketExists: ", errBucketExists)
  373. // if errBucketExists == nil && exists {
  374. // log.Printf("We already own %s\n", bucketName)
  375. // } else {
  376. // log.Fatalln(err)
  377. // return "2", err
  378. // }
  379. // path := utils.UploadDir + time.Now().Format("200601/20060102/")
  380. // path += fileName
  381. // // Upload the zip file with FPutObject
  382. // //contentType := "application/xlsx"
  383. // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  384. // if err != nil {
  385. // log.Fatalln(err)
  386. // return "3", err
  387. // }
  388. //
  389. // path = utils.Imghost + path
  390. // return path, err
  391. //}
  392. type MinioOss struct{}
  393. // UploadFile 上传文件
  394. func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  395. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  396. return "0", errors.New("MinIo信息未配置")
  397. }
  398. ctx := context.Background()
  399. // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
  400. endpoint := utils.MinIoEndpoint
  401. if utils.MinIoBackEndpoint != "" {
  402. endpoint = utils.MinIoBackEndpoint
  403. }
  404. accessKeyID := utils.MinIoAccessKeyId
  405. secretAccessKey := utils.MinIoAccessKeySecret
  406. useSSL := false
  407. if utils.MinIoUseSSL == "true" {
  408. useSSL = true
  409. }
  410. minioClient, err := minio.New(endpoint, &minio.Options{
  411. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  412. Secure: useSSL,
  413. })
  414. if err != nil {
  415. log.Fatalln(err)
  416. return "1", err
  417. }
  418. bucketName := utils.MinIoBucketname
  419. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  420. if errBucketExists != nil || !exists {
  421. err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
  422. return "2", err
  423. }
  424. path := savePath
  425. if savePath == "" {
  426. path = utils.MinIoUploadDir + time.Now().Format("200601/20060102/") + fileName
  427. }
  428. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  429. if err != nil {
  430. log.Fatalln(err)
  431. return "3", err
  432. }
  433. resourceUrl := utils.MinIoImghost + path
  434. return resourceUrl, err
  435. }
  436. func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
  437. token.AccessKeyId = utils.MinIoAccessKeyId
  438. token.SecretKeyId = utils.MinIoAccessKeySecret
  439. token.Endpoint = utils.MinIoEndpoint
  440. token.ImgHost = utils.MinIoImghost
  441. token.Bucketname = utils.MinIoBucketname
  442. token.UseSSL = utils.MinIoUseSSL
  443. token.RegionId = utils.MinIoRegion
  444. token.Port = utils.MinIoPort
  445. return
  446. }