forked from mtusi2026/mtusi2026lab4
feat: add S3 attachments and reconciliation
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("object not found")
|
||||
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
UseTLS bool
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func New(config Config) (*Store, error) {
|
||||
endpoint, err := endpointHost(config.Endpoint, config.UseTLS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(config.Bucket) == "" || config.Timeout <= 0 {
|
||||
return nil, errors.New("invalid object store configuration")
|
||||
}
|
||||
client, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.AccessKey, config.SecretKey, ""),
|
||||
Secure: config.UseTLS,
|
||||
Region: config.Region,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create S3 client: %w", err)
|
||||
}
|
||||
return &Store{client: client, bucket: config.Bucket, timeout: config.Timeout}, nil
|
||||
}
|
||||
|
||||
func endpointHost(value string, useTLS bool) (string, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if !strings.Contains(value, "://") {
|
||||
if value == "" {
|
||||
return "", errors.New("S3 endpoint is required")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
parsed, err := url.Parse(value)
|
||||
if err != nil || parsed.Host == "" || parsed.Path != "" && parsed.Path != "/" {
|
||||
return "", errors.New("invalid S3 endpoint")
|
||||
}
|
||||
if (parsed.Scheme == "https") != useTLS || parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||||
return "", errors.New("S3 endpoint scheme conflicts with S3_USE_TLS")
|
||||
}
|
||||
return parsed.Host, nil
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check S3 bucket: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return errors.New("S3 bucket does not exist")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key, contentType string, data []byte) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
_, err := s.client.PutObject(
|
||||
ctx, s.bucket, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{ContentType: contentType},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("put S3 object: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, objectError("get S3 object", err)
|
||||
}
|
||||
if _, err := object.Stat(); err != nil {
|
||||
cancel()
|
||||
_ = object.Close()
|
||||
return nil, objectError("stat S3 object", err)
|
||||
}
|
||||
return &timeoutReader{ReadCloser: object, cancel: cancel}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Stat(ctx context.Context, key string) (Object, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
info, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
return Object{}, objectError("stat S3 object", err)
|
||||
}
|
||||
return Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
if err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}); err != nil {
|
||||
return objectError("delete S3 object", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, prefix, after string, limit int) ([]Object, string, error) {
|
||||
if limit < 1 || limit > 1000 {
|
||||
return nil, "", errors.New("object list limit must be between 1 and 1000")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
objects := make([]Object, 0, limit+1)
|
||||
options := minio.ListObjectsOptions{Prefix: prefix, Recursive: true, StartAfter: after, MaxKeys: limit + 1}
|
||||
for info := range s.client.ListObjects(ctx, s.bucket, options) {
|
||||
if info.Err != nil {
|
||||
return nil, "", fmt.Errorf("list S3 objects: %w", info.Err)
|
||||
}
|
||||
objects = append(objects, Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
|
||||
if len(objects) == limit+1 {
|
||||
cancel()
|
||||
break
|
||||
}
|
||||
}
|
||||
var next string
|
||||
if len(objects) > limit {
|
||||
objects = objects[:limit]
|
||||
next = objects[len(objects)-1].Key
|
||||
}
|
||||
return objects, next, nil
|
||||
}
|
||||
|
||||
type timeoutReader struct {
|
||||
io.ReadCloser
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (r *timeoutReader) Close() error {
|
||||
err := r.ReadCloser.Close()
|
||||
r.cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
func objectError(action string, err error) error {
|
||||
response := minio.ToErrorResponse(err)
|
||||
if response.Code == "NoSuchKey" || response.Code == "NoSuchObject" {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("%s: %w", action, err)
|
||||
}
|
||||
Reference in New Issue
Block a user