minio.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package oss
  2. import (
  3. "context"
  4. "errors"
  5. "eta_gn/eta_obs/global"
  6. "fmt"
  7. "github.com/minio/minio-go/v7"
  8. "github.com/minio/minio-go/v7/pkg/credentials"
  9. "io"
  10. "log"
  11. "time"
  12. )
  13. type MinioOss struct{}
  14. // UploadFile 上传文件
  15. func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  16. if global.CONFIG.Oss.AccessKeyId == `` || global.CONFIG.Oss.AccessKeySecret == `` {
  17. return "0", errors.New("MinIo信息未配置")
  18. }
  19. ctx := context.Background()
  20. bucketName := global.CONFIG.Oss.BucketName
  21. path := savePath
  22. if savePath == "" {
  23. path = global.CONFIG.Oss.UploadDir + time.Now().Format("200601/20060102/") + fileName
  24. }
  25. _, err := global.MinioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  26. if err != nil {
  27. log.Println(err)
  28. return "3", err
  29. }
  30. resourceUrl := global.CONFIG.Oss.ImgHost + path
  31. return resourceUrl, err
  32. }
  33. func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
  34. useSsl := "1"
  35. if !global.CONFIG.Oss.DisableSSL {
  36. useSsl = `0`
  37. }
  38. token.AccessKeyId = global.CONFIG.Oss.AccessKeyId
  39. token.SecretKeyId = global.CONFIG.Oss.AccessKeySecret
  40. token.Endpoint = global.CONFIG.Oss.EndPoint
  41. token.ImgHost = global.CONFIG.Oss.ImgHost
  42. token.Bucketname = global.CONFIG.Oss.BucketName
  43. token.UseSSL = useSsl
  44. token.RegionId = global.CONFIG.Oss.RegionId
  45. token.Port = global.CONFIG.Oss.Port
  46. return
  47. }
  48. // InitMinio
  49. // @Description: 初始化minio客户端
  50. // @author: Roc
  51. // @datetime 2024-10-25 18:14:10
  52. func InitMinio() {
  53. if global.CONFIG.Oss.AccessKeyId == `` || global.CONFIG.Oss.AccessKeySecret == `` {
  54. panic("MinIo信息未配置")
  55. return
  56. }
  57. ctx := context.Background()
  58. // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
  59. endpoint := global.CONFIG.Oss.EndPoint
  60. if global.CONFIG.Oss.BackEndPoint != "" {
  61. endpoint = global.CONFIG.Oss.BackEndPoint
  62. }
  63. accessKeyID := global.CONFIG.Oss.AccessKeyId
  64. secretAccessKey := global.CONFIG.Oss.AccessKeySecret
  65. useSSL := false
  66. if !global.CONFIG.Oss.DisableSSL {
  67. useSSL = true
  68. }
  69. minioClient, err := minio.New(endpoint, &minio.Options{
  70. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  71. Secure: useSSL,
  72. })
  73. if err != nil {
  74. log.Println("minio链接建立失败", err)
  75. return
  76. }
  77. bucketName := global.CONFIG.Oss.BucketName
  78. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  79. if errBucketExists != nil || !exists {
  80. err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
  81. return
  82. }
  83. global.MinioClient = minioClient
  84. }
  85. // GetFile
  86. // @Description: 读取s3文件的资源
  87. // @author: Roc
  88. // @receiver m
  89. // @datetime 2024-10-25 18:15:58
  90. // @param filePath string
  91. // @return buffer []byte
  92. // @return contentType string
  93. // @return err error
  94. func (m *MinioOss) GetFile(filePath string) (buffer []byte, contentType string, err error) {
  95. if global.MinioClient == nil {
  96. err = errors.New("MinIo客户端未初始化")
  97. return
  98. }
  99. ctx := context.Background()
  100. bucketName := global.CONFIG.Oss.BucketName
  101. // 获取文件元数据
  102. objectInfo, err := global.MinioClient.StatObject(ctx, bucketName, filePath, minio.StatObjectOptions{})
  103. if err != nil {
  104. log.Println("获取源数据失败,err:", err)
  105. return
  106. }
  107. //log.Println(objectInfo)
  108. // 获取文件的 MIME 类型
  109. contentType = objectInfo.ContentType
  110. // 获取对象
  111. obj, err := global.MinioClient.GetObject(ctx, bucketName, filePath, minio.GetObjectOptions{})
  112. if err != nil {
  113. log.Println(err)
  114. return
  115. }
  116. if err != nil {
  117. //log.Println(err)
  118. return
  119. }
  120. defer func() {
  121. _ = obj.Close()
  122. }()
  123. // 读取对象到字节切片
  124. //var buffer []byte
  125. buffer, err = io.ReadAll(obj)
  126. if err != nil {
  127. //log.Println(err)
  128. return
  129. }
  130. return
  131. }