minio.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta_gn/eta_report/utils"
  6. "github.com/minio/minio-go/v7"
  7. "github.com/minio/minio-go/v7/pkg/credentials"
  8. "log"
  9. "os"
  10. "time"
  11. )
  12. // UploadMinIoToDir 上传至hzchart
  13. //func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
  14. // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  15. // return "0", errors.New("MinIo信息未配置")
  16. // }
  17. // ctx := context.Background()
  18. // endpoint := utils.MinIoEndpoint
  19. // accessKeyID := utils.MinIoAccessKeyId
  20. // secretAccessKey := utils.MinIoAccessKeySecret
  21. // useSSL := false
  22. // if utils.MinIoUseSSL == "true" {
  23. // useSSL = true
  24. // }
  25. // minioClient, err := minio.New(endpoint, &minio.Options{
  26. // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  27. // Secure: useSSL,
  28. // })
  29. // if err != nil {
  30. // log.Fatalln(err)
  31. // return "1", err
  32. // }
  33. // bucketName := utils.MinIoBucketname
  34. // // Check to see if we already own this bucket (which happens if you run this twice)
  35. // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  36. // if errBucketExists == nil && exists {
  37. // log.Printf("We already own %s\n", bucketName)
  38. // } else {
  39. // log.Fatalln(err)
  40. // return "2", err
  41. // }
  42. // if uploadDir == "" {
  43. // uploadDir = utils.MinIoUploadDir
  44. // }
  45. // if fileDir == "" {
  46. // fileDir = time.Now().Format("200601/20060102/")
  47. // }
  48. // path := uploadDir + fileDir
  49. // path += filename
  50. // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  51. // if err != nil {
  52. // log.Fatalln(err)
  53. // return "3", err
  54. // }
  55. // path = utils.MinIoImghost + path
  56. // return path, err
  57. //}
  58. type MinioOss struct{}
  59. // UploadFile 上传文件
  60. func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  61. defer func() {
  62. _ = os.Remove(filePath)
  63. }()
  64. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  65. return "0", errors.New("MinIo信息未配置")
  66. }
  67. ctx := context.Background()
  68. endpoint := utils.MinIoEndpoint
  69. accessKeyID := utils.MinIoAccessKeyId
  70. secretAccessKey := utils.MinIoAccessKeySecret
  71. useSSL := false
  72. if utils.MinIoUseSSL == "true" {
  73. useSSL = true
  74. }
  75. minioClient, err := minio.New(endpoint, &minio.Options{
  76. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  77. Secure: useSSL,
  78. })
  79. if err != nil {
  80. log.Fatalln(err)
  81. return "1", err
  82. }
  83. bucketName := utils.MinIoBucketname
  84. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  85. if errBucketExists == nil && exists {
  86. log.Printf("We already own %s\n", bucketName)
  87. } else {
  88. log.Fatalln(err)
  89. return "2", err
  90. }
  91. path := savePath
  92. if savePath == "" {
  93. path = utils.MinIoUploadDir + time.Now().Format("200601/20060102/") + fileName
  94. }
  95. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  96. if err != nil {
  97. log.Fatalln(err)
  98. return "3", err
  99. }
  100. resourceUrl := utils.MinIoImghost + path
  101. return resourceUrl, err
  102. }
  103. func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
  104. token.AccessKeyId = utils.MinIoAccessKeyId
  105. token.SecretKeyId = utils.MinIoAccessKeySecret
  106. token.Endpoint = utils.MinIoEndpoint
  107. token.ImgHost = utils.MinIoImghost
  108. token.Bucketname = utils.MinIoBucketname
  109. token.UseSSL = utils.MinIoUseSSL
  110. token.RegionId = utils.MinIoRegion
  111. token.Port = utils.MinIoPort
  112. return
  113. }