internal/storage keeps files that must outlast the app's disk (exports, uploads) behind one interface: a folder (<data>/files) or an S3-compatible bucket (minio-go, path-style, for Garage). Names that would reach outside the store are refused. An S3 store is checked at start; supermarket config checks the storage and Postgres for real. The same tests run on a folder and on Garage (SUPERMARKET_TEST_S3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"supermarket/internal/config"
|
|
)
|
|
|
|
// S3 keeps files in an S3-compatible bucket (Garage), under a prefix.
|
|
type S3 struct {
|
|
c *minio.Client
|
|
bucket, prefix string // prefix: "" or ending in /
|
|
desc string
|
|
}
|
|
|
|
// OpenS3 connects to the bucket. It doesn't call the server: Check does.
|
|
func OpenS3(s config.Storage) (*S3, error) {
|
|
u, err := url.Parse(s.Endpoint)
|
|
if err != nil || u.Host == "" {
|
|
return nil, fmt.Errorf("storage: the S3 address %q isn't usable", s.Endpoint)
|
|
}
|
|
c, err := minio.New(u.Host, &minio.Options{
|
|
Creds: credentials.NewStaticV4(s.AccessKey, s.SecretKey, ""),
|
|
Secure: u.Scheme == "https",
|
|
Region: s.Region,
|
|
BucketLookup: minio.BucketLookupPath, // bucket in the path: Garage's default, and no wildcard DNS needed
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
prefix := strings.Trim(s.Prefix, "/")
|
|
if prefix != "" {
|
|
prefix += "/"
|
|
}
|
|
return &S3{c: c, bucket: s.Bucket, prefix: prefix, desc: s.String()}, nil
|
|
}
|
|
|
|
func (s *S3) String() string { return s.desc }
|
|
|
|
func (s *S3) object(key string) (string, error) {
|
|
if err := CheckKey(key); err != nil {
|
|
return "", err
|
|
}
|
|
return s.prefix + key, nil
|
|
}
|
|
|
|
// notFound turns S3's "no such key" into ErrNotExist.
|
|
func notFound(err error) error {
|
|
var e minio.ErrorResponse
|
|
if errors.As(err, &e) && (e.Code == "NoSuchKey" || e.StatusCode == http.StatusNotFound) {
|
|
return ErrNotExist
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *S3) Put(ctx context.Context, key string, r io.Reader, size int64) error {
|
|
obj, err := s.object(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = s.c.PutObject(ctx, s.bucket, obj, r, size, minio.PutObjectOptions{ContentType: "application/octet-stream"})
|
|
return err
|
|
}
|
|
|
|
func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
|
obj, err := s.object(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
o, err := s.c.GetObject(ctx, s.bucket, obj, minio.GetObjectOptions{})
|
|
if err != nil {
|
|
return nil, notFound(err)
|
|
}
|
|
// GetObject doesn't ask the server until read: ask now, so a missing
|
|
// file is ErrNotExist here, as with a folder.
|
|
if _, err := o.Stat(); err != nil {
|
|
o.Close()
|
|
return nil, notFound(err)
|
|
}
|
|
return o, nil
|
|
}
|
|
|
|
func (s *S3) Stat(ctx context.Context, key string) (Info, error) {
|
|
obj, err := s.object(key)
|
|
if err != nil {
|
|
return Info{}, err
|
|
}
|
|
oi, err := s.c.StatObject(ctx, s.bucket, obj, minio.StatObjectOptions{})
|
|
if err != nil {
|
|
return Info{}, notFound(err)
|
|
}
|
|
return Info{Key: key, Size: oi.Size, Modified: oi.LastModified}, nil
|
|
}
|
|
|
|
func (s *S3) List(ctx context.Context, prefix string) ([]Info, error) {
|
|
if err := checkPrefix(prefix); err != nil {
|
|
return nil, err
|
|
}
|
|
var out []Info
|
|
for oi := range s.c.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: s.prefix + prefix, Recursive: true}) {
|
|
if oi.Err != nil {
|
|
return nil, oi.Err
|
|
}
|
|
key := strings.TrimPrefix(oi.Key, s.prefix)
|
|
if strings.HasPrefix(key, ".") {
|
|
continue
|
|
}
|
|
out = append(out, Info{Key: key, Size: oi.Size, Modified: oi.LastModified})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *S3) Delete(ctx context.Context, key string) error {
|
|
obj, err := s.object(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.c.RemoveObject(ctx, s.bucket, obj, minio.RemoveObjectOptions{})
|
|
}
|