Add POST /files/presign-upload and /files/complete-upload so clients can PUT files directly to MinIO with upload progress tracking. Co-authored-by: Cursor <cursoragent@cursor.com>
130 lines
3.8 KiB
Go
130 lines
3.8 KiB
Go
package minio
|
|
|
|
import (
|
|
minioclient "Yimaru-Backend/internal/pkgs/minio"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var ErrUploadedObjectNotFound = minioclient.ErrObjectNotFound
|
|
|
|
type Service struct {
|
|
client *minioclient.Client
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewService(client *minioclient.Client, logger *zap.Logger) *Service {
|
|
return &Service{
|
|
client: client,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// Init ensures the bucket exists. Should be called once at startup.
|
|
func (s *Service) Init(ctx context.Context) error {
|
|
return s.client.EnsureBucket(ctx)
|
|
}
|
|
|
|
const defaultPresignedUploadExpiry = 15 * time.Minute
|
|
|
|
// UploadResult holds the result of an upload operation.
|
|
type UploadResult struct {
|
|
ObjectKey string
|
|
}
|
|
|
|
// PresignedUploadResult holds a client-direct MinIO upload slot.
|
|
type PresignedUploadResult struct {
|
|
ObjectKey string
|
|
UploadURL string
|
|
ContentType string
|
|
ExpiresIn int
|
|
}
|
|
|
|
// Upload stores a file in MinIO under the given subdirectory.
|
|
// It generates a unique filename and returns the object key.
|
|
func (s *Service) Upload(ctx context.Context, subDir string, filename string, reader io.Reader, fileSize int64, contentType string) (*UploadResult, error) {
|
|
ext := path.Ext(filename)
|
|
objectKey := subDir + "/" + uuid.New().String() + ext
|
|
|
|
s.logger.Info("Uploading file to MinIO",
|
|
zap.String("object_key", objectKey),
|
|
zap.Int64("size", fileSize),
|
|
zap.String("content_type", contentType),
|
|
)
|
|
|
|
if err := s.client.UploadFile(ctx, objectKey, reader, fileSize, contentType); err != nil {
|
|
s.logger.Error("Failed to upload file to MinIO",
|
|
zap.String("object_key", objectKey),
|
|
zap.Error(err),
|
|
)
|
|
return nil, fmt.Errorf("failed to upload to MinIO: %w", err)
|
|
}
|
|
|
|
s.logger.Info("File uploaded to MinIO successfully", zap.String("object_key", objectKey))
|
|
|
|
return &UploadResult{
|
|
ObjectKey: objectKey,
|
|
}, nil
|
|
}
|
|
|
|
// GetURL returns a presigned URL for the given object key.
|
|
func (s *Service) GetURL(ctx context.Context, objectKey string, expiry time.Duration) (string, error) {
|
|
return s.client.GetFileURL(ctx, objectKey, expiry)
|
|
}
|
|
|
|
// CreatePresignedUpload allocates an object key and returns a presigned PUT URL for direct client upload.
|
|
func (s *Service) CreatePresignedUpload(ctx context.Context, subDir, filename, contentType string) (*PresignedUploadResult, error) {
|
|
ext := path.Ext(filename)
|
|
objectKey := subDir + "/" + uuid.New().String() + ext
|
|
|
|
uploadURL, err := s.client.PresignedPutURL(ctx, objectKey, contentType, defaultPresignedUploadExpiry)
|
|
if err != nil {
|
|
s.logger.Error("Failed to create presigned upload URL",
|
|
zap.String("object_key", objectKey),
|
|
zap.Error(err),
|
|
)
|
|
return nil, fmt.Errorf("failed to create presigned upload URL: %w", err)
|
|
}
|
|
|
|
s.logger.Info("Created presigned MinIO upload URL",
|
|
zap.String("object_key", objectKey),
|
|
zap.String("content_type", contentType),
|
|
)
|
|
|
|
return &PresignedUploadResult{
|
|
ObjectKey: objectKey,
|
|
UploadURL: uploadURL,
|
|
ContentType: contentType,
|
|
ExpiresIn: int(defaultPresignedUploadExpiry.Seconds()),
|
|
}, nil
|
|
}
|
|
|
|
// ConfirmUploadedObject verifies that a presigned upload completed and returns object metadata.
|
|
func (s *Service) ConfirmUploadedObject(ctx context.Context, objectKey string) (int64, string, error) {
|
|
info, err := s.client.StatObject(ctx, objectKey)
|
|
if err != nil {
|
|
if errors.Is(err, minioclient.ErrObjectNotFound) {
|
|
return 0, "", err
|
|
}
|
|
return 0, "", fmt.Errorf("failed to verify uploaded object: %w", err)
|
|
}
|
|
return info.Size, info.ContentType, nil
|
|
}
|
|
|
|
func (s *Service) BucketName() string {
|
|
return s.client.BucketName()
|
|
}
|
|
|
|
// Delete removes a file from MinIO.
|
|
func (s *Service) Delete(ctx context.Context, objectKey string) error {
|
|
s.logger.Info("Deleting file from MinIO", zap.String("object_key", objectKey))
|
|
return s.client.DeleteFile(ctx, objectKey)
|
|
}
|