RezinaLMS-BackEnd/internal/pkgs/minio/client.go
Yared Yemane b57b306372 feat: add MinIO presigned direct upload endpoints
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>
2026-06-14 05:07:59 -07:00

100 lines
2.8 KiB
Go

package minio
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var ErrObjectNotFound = errors.New("object not found")
type Client struct {
mc *minio.Client
bucketName string
}
func (c *Client) BucketName() string {
return c.bucketName
}
func NewClient(endpoint, accessKey, secretKey string, useSSL bool, bucketName string) (*Client, error) {
mc, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: useSSL,
})
if err != nil {
return nil, err
}
return &Client{
mc: mc,
bucketName: bucketName,
}, nil
}
// EnsureBucket creates the bucket if it doesn't exist.
func (c *Client) EnsureBucket(ctx context.Context) error {
exists, err := c.mc.BucketExists(ctx, c.bucketName)
if err != nil {
return err
}
if !exists {
return c.mc.MakeBucket(ctx, c.bucketName, minio.MakeBucketOptions{})
}
return nil
}
// UploadFile uploads a file to MinIO and returns the object key.
func (c *Client) UploadFile(ctx context.Context, objectKey string, reader io.Reader, fileSize int64, contentType string) error {
_, err := c.mc.PutObject(ctx, c.bucketName, objectKey, reader, fileSize, minio.PutObjectOptions{
ContentType: contentType,
})
return err
}
// GetFileURL returns a presigned URL valid for the given duration.
func (c *Client) GetFileURL(ctx context.Context, objectKey string, expiry time.Duration) (string, error) {
u, err := c.mc.PresignedGetObject(ctx, c.bucketName, objectKey, expiry, nil)
if err != nil {
return "", err
}
return u.String(), nil
}
// PresignedPutURL returns a presigned PUT URL so clients can upload directly to MinIO.
func (c *Client) PresignedPutURL(ctx context.Context, objectKey, contentType string, expiry time.Duration) (string, error) {
reqParams := make(url.Values)
if contentType != "" {
reqParams.Set("Content-Type", contentType)
}
u, err := c.mc.Presign(ctx, http.MethodPut, c.bucketName, objectKey, expiry, reqParams)
if err != nil {
return "", err
}
return u.String(), nil
}
// StatObject returns metadata for an object if it exists.
func (c *Client) StatObject(ctx context.Context, objectKey string) (minio.ObjectInfo, error) {
info, err := c.mc.StatObject(ctx, c.bucketName, objectKey, minio.StatObjectOptions{})
if err != nil {
var resp minio.ErrorResponse
if errors.As(err, &resp) && (resp.Code == "NoSuchKey" || resp.StatusCode == http.StatusNotFound) {
return minio.ObjectInfo{}, ErrObjectNotFound
}
return minio.ObjectInfo{}, err
}
return info, nil
}
// DeleteFile removes an object from the bucket.
func (c *Client) DeleteFile(ctx context.Context, objectKey string) error {
return c.mc.RemoveObject(ctx, c.bucketName, objectKey, minio.RemoveObjectOptions{})
}