minio.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_mini_crm_ht/utils"
  6. "fmt"
  7. "github.com/minio/minio-go/v7"
  8. "github.com/minio/minio-go/v7/pkg/credentials"
  9. "time"
  10. )
  11. type MinioOss struct{}
  12. // UploadFile 上传文件
  13. func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  14. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  15. return "0", errors.New("MinIo信息未配置")
  16. }
  17. ctx := context.Background()
  18. // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
  19. endpoint := utils.MinIoEndpoint
  20. if utils.MinIoBackEndpoint != "" {
  21. endpoint = utils.MinIoBackEndpoint
  22. }
  23. accessKeyID := utils.MinIoAccessKeyId
  24. secretAccessKey := utils.MinIoAccessKeySecret
  25. useSSL := false
  26. if utils.MinIoUseSSL == "true" {
  27. useSSL = true
  28. }
  29. minioClient, err := minio.New(endpoint, &minio.Options{
  30. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  31. Secure: useSSL,
  32. })
  33. if err != nil {
  34. utils.FileLog.Error(err.Error())
  35. return "1", err
  36. }
  37. bucketName := utils.MinIoBucketname
  38. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  39. if errBucketExists != nil || !exists {
  40. err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
  41. return "2", err
  42. }
  43. path := savePath
  44. if savePath == "" {
  45. path = utils.MinIoUploadDir + time.Now().Format("200601/20060102/") + fileName
  46. }
  47. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  48. if err != nil {
  49. utils.FileLog.Error(err.Error())
  50. return "3", err
  51. }
  52. resourceUrl := utils.MinIoPdfhost + path
  53. return resourceUrl, err
  54. }
  55. func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
  56. token.AccessKeyId = utils.MinIoAccessKeyId
  57. token.SecretKeyId = utils.MinIoAccessKeySecret
  58. token.Endpoint = utils.MinIoEndpoint
  59. token.ImgHost = utils.MinIoPdfhost
  60. token.Bucketname = utils.MinIoBucketname
  61. token.UseSSL = utils.MinIoUseSSL
  62. token.RegionId = utils.MinIoRegion
  63. token.Port = utils.MinIoPort
  64. return
  65. }
  66. func (m *MinioOss) GetFile(filePath, savePath string) (err error) {
  67. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  68. err = errors.New("MinIo信息未配置")
  69. return
  70. }
  71. ctx := context.Background()
  72. // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
  73. endpoint := utils.MinIoEndpoint
  74. if utils.MinIoBackEndpoint != "" {
  75. endpoint = utils.MinIoBackEndpoint
  76. }
  77. accessKeyID := utils.MinIoAccessKeyId
  78. secretAccessKey := utils.MinIoAccessKeySecret
  79. useSSL := false
  80. if utils.MinIoUseSSL == "true" {
  81. useSSL = true
  82. }
  83. minioClient, err := minio.New(endpoint, &minio.Options{
  84. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  85. Secure: useSSL,
  86. })
  87. if err != nil {
  88. utils.FileLog.Error(err.Error())
  89. return
  90. }
  91. bucketName := utils.MinIoBucketname
  92. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  93. if errBucketExists != nil || !exists {
  94. err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
  95. return
  96. }
  97. err = minioClient.FGetObject(ctx, bucketName, filePath, savePath, minio.GetObjectOptions{})
  98. if err != nil {
  99. utils.FileLog.Error(err.Error())
  100. return
  101. }
  102. return
  103. }
  104. func (m *MinioOss) MultiUploadFile() (err error) {
  105. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  106. err = errors.New("MinIo信息未配置")
  107. return
  108. }
  109. ctx := context.Background()
  110. // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
  111. endpoint := utils.MinIoEndpoint
  112. if utils.MinIoBackEndpoint != "" {
  113. endpoint = utils.MinIoBackEndpoint
  114. }
  115. accessKeyID := utils.MinIoAccessKeyId
  116. secretAccessKey := utils.MinIoAccessKeySecret
  117. useSSL := false
  118. if utils.MinIoUseSSL == "true" {
  119. useSSL = true
  120. }
  121. minioClient, err := minio.New(endpoint, &minio.Options{
  122. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  123. Secure: useSSL,
  124. })
  125. if err != nil {
  126. utils.FileLog.Error(err.Error())
  127. return
  128. }
  129. bucketName := utils.MinIoBucketname
  130. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  131. if errBucketExists != nil || !exists {
  132. err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
  133. return
  134. }
  135. return
  136. }