minio_client.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package file_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/minio/minio-go/v7"
  7. "github.com/minio/minio-go/v7/pkg/credentials"
  8. "log"
  9. "path"
  10. )
  11. type MinioConfig struct {
  12. EndPoint string `json:"endPoint"`
  13. AccessKey string `json:"accessKey"`
  14. SecretKey string `json:"secretKey"`
  15. UseSSL bool `json:"useSSL"`
  16. }
  17. func (c *MinioConfig) validate() error {
  18. if c.EndPoint == "" {
  19. return errors.New("EndPoint host not specified")
  20. }
  21. if c.AccessKey == "" {
  22. return errors.New("minio access key is not specified")
  23. }
  24. if c.SecretKey == "" {
  25. return errors.New("minio secret key is not specified")
  26. }
  27. return nil
  28. }
  29. func (c *MinioConfig) getClient() (*minio.Client, error) {
  30. if err := c.validate(); err != nil {
  31. return nil, err
  32. }
  33. minioClient, err := minio.New(
  34. c.EndPoint,
  35. &minio.Options{
  36. Creds: credentials.NewStaticV4(c.AccessKey, c.SecretKey, ""),
  37. Secure: c.UseSSL,
  38. })
  39. if err != nil {
  40. log.Fatalf("connect minio server fail %s url %s", err.Error(), c.EndPoint)
  41. return nil, err
  42. }
  43. return minioClient, nil
  44. }
  45. type MinioServerOption func(*MinioServe)
  46. type MinioServe struct {
  47. Client *minio.Client
  48. }
  49. func NewMinioServer(opts ...MinioServerOption) (*MinioServe, error) {
  50. m := &MinioServe{}
  51. for _, opt := range opts {
  52. opt(m)
  53. }
  54. return m, nil
  55. }
  56. func (m *MinioServe) Reset() {
  57. m.Client = nil
  58. }
  59. func (m *MinioServe) checkBucket(ctx context.Context, bucketName string) bool {
  60. isExist, err1 := m.Client.BucketExists(ctx, bucketName)
  61. if err1 != nil {
  62. log.Fatalf("checkBucket error " + err1.Error())
  63. return false
  64. }
  65. if !isExist {
  66. err2 := m.Client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: defaultLocation})
  67. if err2 != nil {
  68. log.Fatalf("checkBucket error " + err2.Error())
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. func (m *MinioServe) checkBuckets(ctx context.Context) bool {
  75. b1 := m.checkBucket(ctx, defaultFileBucket)
  76. b2 := m.checkBucket(ctx, defaultIconBucket)
  77. return b1 && b2
  78. }
  79. func (m *MinioServe) uploadFile(objectName, filePath string) (*DestFile, error) {
  80. ctx := context.Background()
  81. bBucketExist := m.checkBuckets(ctx)
  82. if !bBucketExist {
  83. return nil, errors.New("bucket不存在")
  84. }
  85. contentType := "application/octet-stream"
  86. info, err := m.Client.FPutObject(ctx, defaultFileBucket, objectName, filePath, minio.PutObjectOptions{
  87. ContentType: contentType,
  88. })
  89. if err != nil {
  90. return nil, err
  91. }
  92. fileExt := path.Ext(objectName)
  93. url := fmt.Sprintf("http://%s/%s/%s", defaultConfig.EndPoint, defaultFileBucket, objectName)
  94. targetFile := &DestFile{
  95. FileName: objectName,
  96. FileUrl: url,
  97. FileSize: info.Size,
  98. FileExt: fileExt,
  99. FileMimeType: "",
  100. }
  101. return targetFile, nil
  102. }