Files

123 lines
3.5 KiB
Go

// Package objectstore adapts the S3 protocol to bounded application operations.
package objectstore
import (
"context"
"fmt"
"io"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/observation"
)
const operationTimeout = 15 * time.Second
// Client owns a reusable S3 client and its private bucket scope.
type Client struct {
tracer observation.Tracer
client *minio.Client
bucket string
}
// New configures a lazy client with explicit service credentials.
func New(cfg *config.ObjectStore) (*Client, error) {
client, err := minio.New(cfg.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
Secure: cfg.Secure,
})
if err != nil {
return nil, err
}
return &Client{client: client, bucket: cfg.Bucket}, nil
}
// Put uses one bounded PUT for an already validated original.
func (c *Client) Put(ctx context.Context, key, mime string, reader io.Reader, size int64) (result error) {
ctx, end := c.start(ctx, "s3.put")
defer func() { end(result) }()
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
defer cancel()
_, err := c.client.PutObject(ctx, c.bucket, key, reader, size, minio.PutObjectOptions{
ContentType: mime,
DisableMultipart: true,
})
return err
}
// Get verifies object availability before handing its bounded stream to the caller.
func (c *Client) Get(ctx context.Context, key string) (io.ReadCloser, error) {
ctx, end := c.start(ctx, "s3.get")
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
object, err := c.client.GetObject(ctx, c.bucket, key, minio.GetObjectOptions{})
if err != nil {
end(err)
cancel()
return nil, err
}
if _, err := object.Stat(); err != nil {
end(err)
_ = object.Close()
cancel()
return nil, err
}
return &stream{ReadCloser: object, cancel: cancel, end: end}, nil
}
type stream struct {
io.ReadCloser
cancel context.CancelFunc
end func(error)
}
// Close releases the connection and its operation deadline.
func (s *stream) Close() error {
defer s.cancel()
err := s.ReadCloser.Close()
if s.end != nil {
s.end(err)
}
return err
}
// Ping verifies the bucket is accessible with the configured scoped identity.
func (c *Client) Ping(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
defer cancel()
exists, err := c.client.BucketExists(ctx, c.bucket)
if err == nil && !exists {
return fmt.Errorf("private bucket missing")
}
return err
}
// Exists distinguishes a missing object from a failed dependency.
func (c *Client) Exists(ctx context.Context, key string) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
defer cancel()
_, err := c.client.StatObject(ctx, c.bucket, key, minio.StatObjectOptions{})
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
return false, nil
}
return err == nil, err
}
// Remove deletes one explicitly claimed immutable object.
func (c *Client) Remove(ctx context.Context, key string) error {
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
defer cancel()
return c.client.RemoveObject(ctx, c.bucket, key, minio.RemoveObjectOptions{})
}
// Bootstrap creates a bucket idempotently; no anonymous policy is installed.
func (c *Client) Bootstrap(ctx context.Context) error {
exists, err := c.client.BucketExists(ctx, c.bucket)
if err != nil || exists {
return err
}
return c.client.MakeBucket(ctx, c.bucket, minio.MakeBucketOptions{})
}