package storage import ( "context" "fmt" "github.com/minio/minio-go" "net/url" "path" "strings" "time" log "github.com/sirupsen/logrus" ) type MinioConfig struct { Bucket string BaseUrl string Endpoint string AccessKey string SecretKey string Timeout time.Duration } type Minio struct { inited bool config *MinioConfig client *minio.Client } func NewMinio(cfg *MinioConfig) Storage { if cfg.Endpoint == "" { u, _ := url.Parse(cfg.BaseUrl) cfg.Endpoint = u.Host } return &Minio{ config: cfg, inited: false, } } func (s *Minio) Init() error { if s.inited { return nil } c, err := minio.New(s.config.Endpoint, s.config.AccessKey, s.config.SecretKey, false) if err != nil { return err } s.client = c s.inited = true return nil } func (s *Minio) Put(fileName, objectName string, onProcess func(fsize, uploaded int64)) error { if err := s.Init(); err != nil { return err } ctx, cancel := context.WithTimeout(context.Background(), s.config.Timeout) defer cancel() objectName = strings.TrimLeft(objectName, "/ ") ext := strings.TrimLeft(path.Ext(fileName), ".") _, err := s.client.FPutObjectWithContext(ctx, s.config.Bucket, objectName, fileName, minio.PutObjectOptions{ContentType: ext2ContentType(ext)}) if err != nil { log.Errorf("upload file to minio error:%s", err.Error()) return err } return nil } func (s *Minio) Del(objectName string) error { if err := s.Init(); err != nil { return err } err := s.client.RemoveObject(s.config.Bucket, objectName) if err != nil { log.Errorf("delete file to minio error:%s", err.Error()) return err } return nil } func (s *Minio) Get(objectName, fileName string) error { if err := s.Init(); err != nil { return err } return s.client.FGetObject(s.config.Bucket, objectName, fileName, minio.GetObjectOptions{}) } func (s *Minio) List(objectPrefix string) ([]string, error) { if err := s.Init(); err != nil { return nil, err } done := make(chan struct{}) defer close(done) infos := s.client.ListObjectsV2(s.config.Bucket, objectPrefix, true, done) result := make([]string, 0) for info := range infos { result = append(result, info.Key) } return result, nil } func (s *Minio) Url(objectName string, expire time.Duration) string { if err := s.Init(); err != nil { return err.Error() } var params url.Values u, err := s.client.PresignedGetObject(s.config.Bucket, objectName, expire, params) if err != nil { return fmt.Sprintf("%s/%s/%s", s.config.BaseUrl, s.config.Bucket, objectName) } return u.String() } func (s *Minio) Stat(objectName string) (*ObjectInfo, error) { return nil, nil } func (s *Minio) Fetch(url, objectName string) error { return nil }