Files
mtusi2026lab5/internal/platform/config/s3.go
T

67 lines
1.9 KiB
Go

package config
import (
"fmt"
"math"
"strconv"
"time"
)
type S3 struct {
Endpoint, Region, Bucket string
AccessKey, SecretKey string
MaxBytes, MaxPixels int64
StagingRetention time.Duration
}
func LoadS3(lookup Lookup) (S3, error) {
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000, StagingRetention: 24 * time.Hour}
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
if v, ok := lookup(key); ok {
*dest = v
}
}
var err error
if cfg.AccessKey, err = secret(lookup, "S3_ACCESS_KEY_ID_FILE"); err != nil {
return S3{}, err
}
if cfg.SecretKey, err = secret(lookup, "S3_SECRET_ACCESS_KEY_FILE"); err != nil {
return S3{}, err
}
for _, p := range []struct {
key string
dest *int64
max int64
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, 10485760}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, 40000000}} {
if v, ok := lookup(p.key); ok {
n, e := strconv.ParseInt(v, 10, 64)
if e != nil || n < 1 || n > p.max {
return S3{}, fmt.Errorf("%s must be between 1 and %d", p.key, p.max)
}
*p.dest = n
}
}
if raw, ok := lookup("STAGING_RETENTION_SECONDS"); ok {
const maxSeconds = math.MaxInt64 / int64(time.Second)
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil || n < 1 || n > maxSeconds {
return S3{}, fmt.Errorf("STAGING_RETENTION_SECONDS must be between 1 and %d", maxSeconds)
}
cfg.StagingRetention = time.Duration(n) * time.Second
}
return cfg, nil
}
// ReconcileS3 selects the separate listing identity used by background scans.
func ReconcileS3(lookup Lookup) Lookup {
return func(key string) (string, bool) {
if key == "S3_ACCESS_KEY_ID_FILE" {
return lookup("S3_RECONCILE_ACCESS_KEY_ID_FILE")
}
if key == "S3_SECRET_ACCESS_KEY_FILE" {
return lookup("S3_RECONCILE_SECRET_ACCESS_KEY_FILE")
}
return lookup(key)
}
}