67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
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
|
|
} |