feat(lab4): MinIO S3 private bucket, streaming image validation, attachment endpoints, Grade 5 Track 2 reconciler
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package s3store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrObjectNotFound = errors.New("s3 object not found")
|
||||
ErrS3Unavailable = errors.New("s3 storage unavailable")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
}
|
||||
|
||||
type ObjectSummary struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
mc *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewClient(cfg Config) (*Client, error) {
|
||||
u, err := url.Parse(strings.TrimSpace(cfg.Endpoint))
|
||||
if err != nil || u.Host == "" {
|
||||
return nil, fmt.Errorf("invalid S3 endpoint %q", cfg.Endpoint)
|
||||
}
|
||||
useSSL := strings.EqualFold(u.Scheme, "https")
|
||||
region := strings.TrimSpace(cfg.Region)
|
||||
if region == "" {
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
tr := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 800 * time.Millisecond,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 2 * time.Second,
|
||||
ResponseHeaderTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
mc, err := minio.New(u.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: useSSL,
|
||||
Region: region,
|
||||
BucketLookup: minio.BucketLookupPath,
|
||||
Transport: tr,
|
||||
MaxRetries: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create minio client: %w", err)
|
||||
}
|
||||
|
||||
return &Client{
|
||||
mc: mc,
|
||||
bucket: cfg.Bucket,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Bucket() string {
|
||||
return c.bucket
|
||||
}
|
||||
|
||||
func (c *Client) Ready(ctx context.Context) error {
|
||||
rCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
exists, err := c.mc.BucketExists(rCtx, c.bucket)
|
||||
if err != nil || !exists {
|
||||
return ErrS3Unavailable
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) PutObject(ctx context.Context, objectKey string, r io.Reader, size int64, contentType string) error {
|
||||
pCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := c.mc.PutObject(pCtx, c.bucket, objectKey, r, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrS3Unavailable, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, int64, error) {
|
||||
obj, err := c.mc.GetObject(ctx, c.bucket, objectKey, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("%w: %v", ErrS3Unavailable, err)
|
||||
}
|
||||
info, err := obj.Stat()
|
||||
if err != nil {
|
||||
_ = obj.Close()
|
||||
errResp := minio.ToErrorResponse(err)
|
||||
if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 {
|
||||
return nil, 0, ErrObjectNotFound
|
||||
}
|
||||
return nil, 0, fmt.Errorf("%w: %v", ErrS3Unavailable, err)
|
||||
}
|
||||
return obj, info.Size, nil
|
||||
}
|
||||
|
||||
func (c *Client) StatObject(ctx context.Context, objectKey string) (bool, error) {
|
||||
sCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
_, err := c.mc.StatObject(sCtx, c.bucket, objectKey, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
errResp := minio.ToErrorResponse(err)
|
||||
if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("%w: %v", ErrS3Unavailable, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *Client) RemoveObject(ctx context.Context, objectKey string) error {
|
||||
dCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := c.mc.RemoveObject(dCtx, c.bucket, objectKey, minio.RemoveObjectOptions{}); err != nil {
|
||||
errResp := minio.ToErrorResponse(err)
|
||||
if errResp.Code == "NoSuchKey" || errResp.StatusCode == 404 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%w: %v", ErrS3Unavailable, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) ListObjects(ctx context.Context, prefix string) ([]ObjectSummary, error) {
|
||||
lCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
var out []ObjectSummary
|
||||
for obj := range c.mc.ListObjects(lCtx, c.bucket, minio.ListObjectsOptions{
|
||||
Prefix: prefix,
|
||||
Recursive: true,
|
||||
}) {
|
||||
if obj.Err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrS3Unavailable, obj.Err)
|
||||
}
|
||||
out = append(out, ObjectSummary{
|
||||
Key: obj.Key,
|
||||
Size: obj.Size,
|
||||
LastModified: obj.LastModified,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user