forked from mtusi2026/mtusi2026lab4
202 lines
5.3 KiB
Go
202 lines
5.3 KiB
Go
package objectstore
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"mtusi2026lab4/internal/application"
|
|
|
|
minio "github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
type MinIOStore struct {
|
|
client *minio.Client
|
|
bucket string
|
|
operationTimeout time.Duration
|
|
}
|
|
|
|
type StoredObject struct {
|
|
Key string
|
|
SizeBytes int64
|
|
LastModified time.Time
|
|
}
|
|
|
|
func NewMinIOStore(
|
|
endpoint string,
|
|
region string,
|
|
bucket string,
|
|
accessKeyID string,
|
|
secretAccessKey string,
|
|
operationTimeout time.Duration,
|
|
) (*MinIOStore, error) {
|
|
parsedEndpoint, err := url.Parse(endpoint)
|
|
if err != nil || parsedEndpoint.Host == "" {
|
|
return nil, fmt.Errorf("S3 endpoint must be an absolute HTTP(S) URL")
|
|
}
|
|
if parsedEndpoint.Scheme != "http" && parsedEndpoint.Scheme != "https" {
|
|
return nil, fmt.Errorf("S3 endpoint scheme must be http or https")
|
|
}
|
|
if parsedEndpoint.Path != "" && parsedEndpoint.Path != "/" {
|
|
return nil, fmt.Errorf("S3 endpoint must not contain a path")
|
|
}
|
|
if parsedEndpoint.RawQuery != "" || parsedEndpoint.Fragment != "" || parsedEndpoint.User != nil {
|
|
return nil, fmt.Errorf("S3 endpoint must not contain credentials, query or fragment")
|
|
}
|
|
if strings.TrimSpace(bucket) == "" || strings.TrimSpace(accessKeyID) == "" ||
|
|
strings.TrimSpace(secretAccessKey) == "" {
|
|
return nil, fmt.Errorf("S3 bucket and credentials are required")
|
|
}
|
|
if operationTimeout <= 0 {
|
|
return nil, fmt.Errorf("S3 operation timeout must be positive")
|
|
}
|
|
|
|
client, err := minio.New(parsedEndpoint.Host, &minio.Options{
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
Secure: parsedEndpoint.Scheme == "https",
|
|
Region: region,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create MinIO client: %w", err)
|
|
}
|
|
return &MinIOStore{client: client, bucket: bucket, operationTimeout: operationTimeout}, nil
|
|
}
|
|
|
|
func (store *MinIOStore) Put(ctx context.Context, object application.ObjectPut) error {
|
|
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
|
defer cancel()
|
|
upload, err := store.client.PutObject(
|
|
operationContext,
|
|
store.bucket,
|
|
object.Key,
|
|
object.Body,
|
|
object.SizeBytes,
|
|
minio.PutObjectOptions{ContentType: object.ContentType},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("put MinIO object: %w", err)
|
|
}
|
|
if upload.Size != object.SizeBytes {
|
|
return fmt.Errorf("put MinIO object: stored %d bytes, want %d", upload.Size, object.SizeBytes)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MinIOStore) Get(
|
|
ctx context.Context,
|
|
key string,
|
|
) (application.ObjectContent, error) {
|
|
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
|
stat, err := store.client.StatObject(
|
|
operationContext,
|
|
store.bucket,
|
|
key,
|
|
minio.StatObjectOptions{},
|
|
)
|
|
if err != nil {
|
|
cancel()
|
|
return application.ObjectContent{}, fmt.Errorf("stat MinIO object: %w", err)
|
|
}
|
|
object, err := store.client.GetObject(
|
|
operationContext,
|
|
store.bucket,
|
|
key,
|
|
minio.GetObjectOptions{},
|
|
)
|
|
if err != nil {
|
|
cancel()
|
|
return application.ObjectContent{}, fmt.Errorf("get MinIO object: %w", err)
|
|
}
|
|
firstByte := make([]byte, 1)
|
|
readCount, readError := object.Read(firstByte)
|
|
if readError != nil && readCount == 0 {
|
|
_ = object.Close()
|
|
cancel()
|
|
return application.ObjectContent{}, fmt.Errorf("read MinIO object: %w", readError)
|
|
}
|
|
if readCount != 1 {
|
|
_ = object.Close()
|
|
cancel()
|
|
return application.ObjectContent{}, fmt.Errorf("read MinIO object: expected non-empty object")
|
|
}
|
|
return application.ObjectContent{
|
|
Body: &cancelingReadCloser{
|
|
Reader: io.MultiReader(bytes.NewReader(firstByte), object),
|
|
Closer: object,
|
|
cancel: cancel,
|
|
},
|
|
SizeBytes: stat.Size,
|
|
}, nil
|
|
}
|
|
|
|
func (store *MinIOStore) List(
|
|
ctx context.Context,
|
|
prefix string,
|
|
) ([]StoredObject, error) {
|
|
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
|
defer cancel()
|
|
|
|
objects := make([]StoredObject, 0)
|
|
for object := range store.client.ListObjects(
|
|
operationContext,
|
|
store.bucket,
|
|
minio.ListObjectsOptions{Prefix: prefix, Recursive: true},
|
|
) {
|
|
if object.Err != nil {
|
|
return nil, fmt.Errorf("list MinIO objects: %w", object.Err)
|
|
}
|
|
objects = append(objects, StoredObject{
|
|
Key: object.Key,
|
|
SizeBytes: object.Size,
|
|
LastModified: object.LastModified.UTC(),
|
|
})
|
|
}
|
|
return objects, nil
|
|
}
|
|
|
|
func (store *MinIOStore) Delete(ctx context.Context, key string) error {
|
|
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
|
defer cancel()
|
|
if err := store.client.RemoveObject(
|
|
operationContext,
|
|
store.bucket,
|
|
key,
|
|
minio.RemoveObjectOptions{},
|
|
); err != nil {
|
|
return fmt.Errorf("delete MinIO object: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *MinIOStore) Ping(ctx context.Context) error {
|
|
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
|
defer cancel()
|
|
exists, err := store.client.BucketExists(operationContext, store.bucket)
|
|
if err != nil {
|
|
return fmt.Errorf("check MinIO bucket: %w", err)
|
|
}
|
|
if !exists {
|
|
return fmt.Errorf("MinIO bucket %q does not exist", store.bucket)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type cancelingReadCloser struct {
|
|
io.Reader
|
|
io.Closer
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func (reader *cancelingReadCloser) Close() error {
|
|
err := reader.Closer.Close()
|
|
reader.cancel()
|
|
return err
|
|
}
|
|
|
|
var _ application.ObjectStore = (*MinIOStore)(nil)
|