add media service
This commit is contained in:
67
internal/storage/minio.go
Normal file
67
internal/storage/minio.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type MinioStorage struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewMinioStorage(endpoint, accessKey, secretKey, bucket string) (*MinioStorage, error) {
|
||||
client, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Secure: false, // Для локальной разработки (без HTTPS)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
exists, err := client.BucketExists(ctx, bucket)
|
||||
if err == nil && !exists {
|
||||
err = client.MakeBucket(ctx, bucket, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
slog.Info("Бакет создан", "bucket", bucket)
|
||||
// Бакет остается ПРИВАТНЫМ (нет публичного BucketPolicy).
|
||||
}
|
||||
|
||||
return &MinioStorage{
|
||||
client: client,
|
||||
bucket: bucket,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Upload(ctx context.Context, objectName string, reader io.Reader, size int64, contentType string) (string, error) {
|
||||
info, err := s.client.PutObject(ctx, s.bucket, objectName, reader, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Возвращаем ТОЛЬКО имя файла (info.Key).
|
||||
// Полный путь клиенту не нужен, он сам не сможет по нему перейти.
|
||||
return info.Key, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) GeneratePresignedURL(ctx context.Context, objectName string, expiry time.Duration) (string, error) {
|
||||
reqParams := make(url.Values)
|
||||
|
||||
presignedURL, err := s.client.PresignedGetObject(ctx, s.bucket, objectName, expiry, reqParams)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user