minio.go 978 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package minio
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/minio/minio-go/v7"
  6. "github.com/minio/minio-go/v7/pkg/credentials"
  7. "io"
  8. "iot_manager_service/config"
  9. "iot_manager_service/util/logger"
  10. )
  11. type FileObject struct {
  12. Bucket string
  13. ObjectName string
  14. ObjectSize int64
  15. Reader io.Reader
  16. }
  17. var minioClient *minio.Client
  18. func InitMinio() {
  19. cfg := config.Instance()
  20. client, err := minio.New(cfg.Minio.Endpoint, &minio.Options{
  21. Creds: credentials.NewStaticV4(cfg.Minio.AccessKey, cfg.Minio.SecretKey, ""),
  22. Secure: false,
  23. })
  24. if err != nil {
  25. panic(fmt.Sprintf("InitMinio err = %s", err.Error()))
  26. }
  27. minioClient = client
  28. }
  29. func PutFile(fileObject FileObject) error {
  30. ctx := context.Background()
  31. _, err := minioClient.PutObject(ctx, fileObject.Bucket, fileObject.ObjectName, fileObject.Reader, fileObject.ObjectSize,
  32. minio.PutObjectOptions{})
  33. if err != nil {
  34. logger.Logger.Errorf("PutObject fail, err = %s", err.Error())
  35. return err
  36. }
  37. return nil
  38. }