Files
geo/server/internal/shared/objectstorage/client.go
T
root db95b8e4ee feat(kol): store KOL prompt content in database with object storage fallback
Add prompt_content columns to kol_prompts and kol_prompt_revisions so
prompt bodies live alongside their metadata, eliminating an extra round
trip to object storage for the common read path while keeping the old
asset key as a fallback for legacy rows.

Reads go through a singleflight-deduped, cache-backed loader that
prefers the database column, falls back to the asset key, and tolerates
missing objects via a new objectstorage.ErrObjectNotFound returned by
both the Aliyun OSS and MinIO clients on 404/NoSuchKey responses.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 01:47:51 +08:00

70 lines
1.8 KiB
Go

package objectstorage
import (
"context"
"errors"
"fmt"
"strings"
"go.uber.org/zap"
"github.com/geo-platform/tenant-api/internal/shared/config"
)
var ErrNotConfigured = errors.New("object storage is not configured")
var ErrObjectNotFound = errors.New("object storage object not found")
type Client interface {
Validate() error
PutBytes(ctx context.Context, objectKey string, content []byte, contentType string) error
GetBytes(ctx context.Context, objectKey string) ([]byte, error)
Exists(ctx context.Context, objectKey string) (bool, error)
Delete(ctx context.Context, objectKey string) error
PublicURL(objectKey string) (string, error)
}
func New(cfg config.ObjectStorageConfig, logger *zap.Logger) Client {
provider := strings.ToLower(strings.TrimSpace(cfg.Provider))
switch provider {
case "", "disabled":
return disabledClient{reason: "object storage is disabled"}
case "minio", "mino":
return NewMinIOClient(cfg, logger)
case "aliyun", "aliyun_oss", "aliyun-oss", "oss":
return NewAliyunClient(cfg, logger)
default:
return disabledClient{reason: fmt.Sprintf("unsupported object storage provider %q", cfg.Provider)}
}
}
type disabledClient struct {
reason string
}
func (c disabledClient) Validate() error {
if c.reason == "" {
c.reason = "missing object storage configuration"
}
return fmt.Errorf("%w: %s", ErrNotConfigured, c.reason)
}
func (c disabledClient) PutBytes(context.Context, string, []byte, string) error {
return c.Validate()
}
func (c disabledClient) GetBytes(context.Context, string) ([]byte, error) {
return nil, c.Validate()
}
func (c disabledClient) Exists(context.Context, string) (bool, error) {
return false, c.Validate()
}
func (c disabledClient) Delete(context.Context, string) error {
return c.Validate()
}
func (c disabledClient) PublicURL(string) (string, error) {
return "", c.Validate()
}