53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
|
|
package mediaStorage
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"errors"
|
|||
|
|
"fmt"
|
|||
|
|
"github.com/minio/minio-go/v7"
|
|||
|
|
log "github.com/sirupsen/logrus"
|
|||
|
|
"io"
|
|||
|
|
"net/url"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type Service struct {
|
|||
|
|
client *minio.Client
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func newService(client *minio.Client) *Service {
|
|||
|
|
return &Service{
|
|||
|
|
client: client,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Service) СreateBucketIfNotExists(bucketName string) error {
|
|||
|
|
ctx := context.Background()
|
|||
|
|
err := s.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
|
|||
|
|
if err != nil {
|
|||
|
|
var minioErr minio.ErrorResponse
|
|||
|
|
if errors.As(err, &minioErr) {
|
|||
|
|
if minioErr.Code == "BucketAlreadyExists" || minioErr.Code == "BucketAlreadyOwnedByYou" {
|
|||
|
|
log.Infof("Media storage | Bucket %s already exists, skipping creation", bucketName)
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return fmt.Errorf("failed to create bucket: %w", err)
|
|||
|
|
}
|
|||
|
|
log.Infof("Media storage | Bucket %s created successfully", bucketName)
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error {
|
|||
|
|
_, err := s.client.PutObject(ctx, bucket, object, reader, size, minio.PutObjectOptions{ContentType: "image/jpeg"})
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) {
|
|||
|
|
return s.client.PresignedGetObject(ctx, bucket, object, expires, params)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Service) Delete(ctx context.Context, bucket, object string) error {
|
|||
|
|
return s.client.RemoveObject(ctx, bucket, object, minio.RemoveObjectOptions{})
|
|||
|
|
}
|