This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab7/internal/application"
|
||||
|
||||
minio "github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
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 {
|
||||
ctx, span := startObjectSpan(ctx, "put")
|
||||
defer span.End()
|
||||
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) {
|
||||
ctx, span := startObjectSpan(ctx, "get")
|
||||
defer span.End()
|
||||
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) {
|
||||
ctx, span := startObjectSpan(ctx, "list")
|
||||
defer span.End()
|
||||
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 {
|
||||
ctx, span := startObjectSpan(ctx, "delete")
|
||||
defer span.End()
|
||||
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 {
|
||||
ctx, span := startObjectSpan(ctx, "bucket_exists")
|
||||
defer span.End()
|
||||
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
|
||||
}
|
||||
|
||||
func startObjectSpan(ctx context.Context, operation string) (context.Context, trace.Span) {
|
||||
return otel.Tracer("mtusi2026lab7/s3").Start(
|
||||
ctx,
|
||||
"s3."+operation,
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(
|
||||
attribute.String("storage.system", "s3"),
|
||||
attribute.String("storage.operation", operation),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,37 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewMinIOStoreValidatesConfiguration(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
endpoint string
|
||||
bucket string
|
||||
accessKey string
|
||||
secretKey string
|
||||
timeout time.Duration
|
||||
wantError bool
|
||||
}{
|
||||
{name: "valid HTTP", endpoint: "http://minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second},
|
||||
{name: "valid HTTPS", endpoint: "https://s3.example.test", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second},
|
||||
{name: "relative endpoint", endpoint: "minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "endpoint path", endpoint: "http://minio:9000/path", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "embedded credentials", endpoint: "http://user:pass@minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "empty bucket", endpoint: "http://minio:9000", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "empty credentials", endpoint: "http://minio:9000", bucket: "attachments", timeout: time.Second, wantError: true},
|
||||
{name: "invalid timeout", endpoint: "http://minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", wantError: true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
_, err := NewMinIOStore(testCase.endpoint, "us-east-1", testCase.bucket,
|
||||
testCase.accessKey, testCase.secretKey, testCase.timeout)
|
||||
if (err != nil) != testCase.wantError {
|
||||
t.Fatalf("NewMinIOStore() error = %v, wantError %v", err, testCase.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user