objectstorex

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 6 Imported by: 0

README

objectstorex

objectstorex 是 Aisphere Kernel 的统一对象存储模块。基于 Minio(minio-go/v7),兼容所有 S3 协议的服务(AWS S3、Minio、DigitalOcean Spaces 等)。

快速上手

import (
    "github.com/aisphereio/kernel/objectstorex"
    _ "github.com/aisphereio/kernel/objectstorex/minio"
)

store, _ := objectstorex.New(objectstorex.Config{
    Driver:       "minio",
    Endpoint:     "localhost:9000",
    UseSSL:       false,
    AccessKey:    "minioadmin",
    SecretKey:    "minioadmin",
    Bucket:       "aihub",
    EnsureBucket: true, // 启动时自动建桶
})
defer store.Close()

// 上传
store.PutObject(ctx, "skills/demo/package.zip", reader, size, objectstorex.PutOptions{
    ContentType: "application/zip",
    Metadata:    map[string]string{"skill-name": "demo"},
})

// 下载
rc, info, _ := store.GetObject(ctx, "skills/demo/package.zip", objectstorex.GetOptions{})
defer rc.Close()

// 预签名 URL(给前端直传)
url, _ := store.PresignPut(ctx, "skills/demo/upload.zip", 15*time.Minute)

// 删除
store.DeleteObject(ctx, "skills/demo/package.zip")

API

类别 方法
Bucket 管理 Bucket / BucketExists / EnsureBucket
CRUD PutObject / GetObject / DeleteObject / StatObject
列表 ListObjects (prefix 过滤)
复制 CopyObject
预签名 PresignPut / PresignGet
分片上传 InitMultipartUpload / UploadPart / CompleteMultipartUpload / AbortMultipartUpload
生命周期 Close

测试

# 单元测试(fake 内存实现,不需要 Docker)
go test ./objectstorex/... -short

# 集成测试(testcontainers,需要 Docker)
go test ./objectstorex/...

# 或用外部 Minio
KERNEL_OBJECTSTOREX_MINIO_ENDPOINT=localhost:9000 go test ./objectstorex/... -run TestIntegration

Documentation

Overview

Package objectstorex provides the unified object storage API for Aisphere Kernel.

objectstorex is the ONLY object storage abstraction that business code should depend on. It exposes a stable Client interface backed by Minio (which is S3-compatible), with context propagation, presign URL, and multipart upload support.

Quickstart

import (
    "github.com/aisphereio/kernel/objectstorex"
    _ "github.com/aisphereio/kernel/objectstorex/minio" // register "minio" driver
)

store, err := objectstorex.New(objectstorex.Config{
    Driver:   "minio",
    Endpoint: "localhost:9000",
    UseSSL:   false,
    AccessKey: "minioadmin",
    SecretKey: "minioadmin",
    Bucket:   "aihub",
    Region:   "us-east-1",
})
if err != nil { return err }
defer store.Close()

// Upload
_, err = store.PutObject(ctx, "skills/demo/package.zip", bytes.NewReader(data), int64(len(data)),
    objectstorex.PutOptions{ContentType: "application/zip"})

// Download
rc, info, err := store.GetObject(ctx, "skills/demo/package.zip", objectstorex.GetOptions{})
defer rc.Close()

// Presign URL for frontend direct upload
url, err := store.PresignPut(ctx, "skills/demo/package.zip", 15*time.Minute)

Drivers

import _ "github.com/aisphereio/kernel/objectstorex/minio" // registers "minio"

The minio driver uses github.com/minio/minio-go/v7 and is compatible with any S3-compatible service (AWS S3, Minio, DigitalOcean Spaces, etc.).

Forbidden patterns

Do not import `github.com/minio/minio-go/v7` in business code. Use the objectstorex.Client interface.

Index

Constants

View Source
const (
	CodeNotFound        = errorx.Code("OBJECTSTOREX_NOT_FOUND")
	CodeInvalidConfig   = errorx.Code("OBJECTSTOREX_INVALID_CONFIG")
	CodeUnknownDriver   = errorx.Code("OBJECTSTOREX_UNKNOWN_DRIVER")
	CodeBucketNotExists = errorx.Code("OBJECTSTOREX_BUCKET_NOT_EXISTS")
	CodeClosed          = errorx.Code("OBJECTSTOREX_CLOSED")
	CodeOperationFailed = errorx.Code("OBJECTSTOREX_OPERATION_FAILED")
	CodeTimeout         = errorx.Code("OBJECTSTOREX_TIMEOUT")
)

Variables

View Source
var (
	// ErrNotFound is returned by GetObject / StatObject when the key does not exist.
	ErrNotFound = errors.New("objectstorex: object not found")

	// ErrNilConfig is returned by New when Config is missing required fields.
	ErrNilConfig = errors.New("objectstorex: config is missing required fields")

	// ErrUnknownDriver is returned when the driver name has not been registered.
	ErrUnknownDriver = errors.New("objectstorex: unknown driver (did you import objectstorex/minio?)")

	// ErrBucketNotExists is returned when the configured bucket does not exist.
	ErrBucketNotExists = errors.New("objectstorex: bucket does not exist")

	// ErrClosed is returned when a closed store is used.
	ErrClosed = errors.New("objectstorex: store is closed")
)

Public sentinel errors.

Functions

func IsDriverRegistered

func IsDriverRegistered(name string) bool

IsDriverRegistered returns true if the named driver has been registered.

func NormalizeError added in v0.0.5

func NormalizeError(err error) error

func RegisterDriver

func RegisterDriver(name string, fn DriverOpener)

RegisterDriver registers a driver opener under the given name.

func RegisteredDrivers

func RegisteredDrivers() []string

RegisteredDrivers returns the names of all registered drivers.

Types

type Client

type Client interface {

	// Bucket returns the default bucket name.
	Bucket() string

	// BucketExists checks if the configured bucket exists.
	BucketExists(ctx context.Context) (bool, error)

	// EnsureBucket creates the bucket if it doesn't exist. Idempotent.
	EnsureBucket(ctx context.Context) error

	// PutObject uploads an object. body is the reader; size is the content
	// length (use -1 if unknown). Returns ObjectInfo on success.
	PutObject(ctx context.Context, key string, body ReadSeeker, size int64, opts PutOptions) (ObjectInfo, error)

	// GetObject downloads an object. Returns a ReadCloser (caller must close)
	// and ObjectInfo. Returns ErrNotFound if the key doesn't exist.
	GetObject(ctx context.Context, key string, opts GetOptions) (ReadCloser, ObjectInfo, error)

	// DeleteObject removes an object. No-op if the key doesn't exist.
	DeleteObject(ctx context.Context, key string) error

	// StatObject returns metadata about an object without downloading it.
	StatObject(ctx context.Context, key string) (ObjectInfo, error)

	// ListObjects lists objects in the bucket matching the prefix.
	ListObjects(ctx context.Context, opts ListOptions) ([]ObjectInfo, error)

	// CopyObject copies an object from srcKey to dstKey.
	CopyObject(ctx context.Context, srcKey, dstKey string, opts PutOptions) (ObjectInfo, error)

	// PresignPut generates a presigned URL for uploading an object directly
	// from the browser. The URL expires after expiry.
	PresignPut(ctx context.Context, key string, expiry time.Duration) (string, error)

	// PresignGet generates a presigned URL for downloading an object directly
	// from the browser. The URL expires after expiry.
	PresignGet(ctx context.Context, key string, expiry time.Duration) (string, error)

	// InitMultipartUpload starts a multipart upload session.
	// Returns an upload ID that must be passed to UploadPart and CompleteMultipartUpload.
	InitMultipartUpload(ctx context.Context, key string, opts PutOptions) (uploadID string, err error)

	// UploadPart uploads a single part of a multipart upload.
	// partNumber is 1-indexed. Returns the ETag of the part.
	UploadPart(ctx context.Context, key, uploadID string, partNumber int, body ReadSeeker, size int64) (etag string, err error)

	// CompleteMultipartUpload finalizes a multipart upload by assembling
	// all uploaded parts into the final object.
	CompleteMultipartUpload(ctx context.Context, key, uploadID string, parts []CompletedPart) (ObjectInfo, error)

	// AbortMultipartUpload cancels a multipart upload and discards uploaded parts.
	AbortMultipartUpload(ctx context.Context, key, uploadID string) error

	// Close closes the underlying client. Idempotent.
	Close() error

	// DriverName returns the registered driver name.
	DriverName() string
}

Client is the runtime object store interface used by kernel modules and apps.

All methods accept context.Context. It mirrors the aisphere-hub objectstore.Client interface exactly so existing code can adopt objectstorex with minimal changes.

func New

func New(cfg Config) (Client, error)

New opens an object store connection using the supplied Config.

type CompletedPart

type CompletedPart struct {
	PartNumber int
	ETag       string
}

CompletedPart represents an uploaded part in a multipart upload.

type Config

type Config struct {
	// Driver selects the registered driver: "minio".
	Driver string `json:"driver"`

	// Endpoint is the S3-compatible endpoint URL.
	// Minio: "localhost:9000"
	// AWS S3: "s3.us-east-1.amazonaws.com"
	Endpoint string `json:"endpoint"`

	// UseSSL enables HTTPS.
	UseSSL bool `json:"use_ssl"`

	// AccessKey is the access key ID.
	AccessKey string `json:"access_key"`

	// SecretKey is the secret access key.
	SecretKey string `json:"secret_key"`

	// Bucket is the default bucket name.
	Bucket string `json:"bucket"`

	// Region is the S3 region (leave empty for Minio).
	Region string `json:"region"`

	// EnsureBucket, if true, creates the bucket on startup if it doesn't exist.
	EnsureBucket bool `json:"ensure_bucket"`

	// PresignExpiry is the default expiry for presigned URLs.
	// Zero means 15 minutes.
	PresignExpiry time.Duration `json:"presign_expiry_ns"`

	// Logger is the component logger. If nil, objectstorex uses logx.DefaultLogger().
	Logger logx.Logger `json:"-" yaml:"-"`

	// Metrics is the optional metrics manager for object store operations.
	Metrics metricsx.Manager `json:"-" yaml:"-"`

	// MetricsEnabled controls whether object store operations record metrics.
	MetricsEnabled bool `json:"metrics_enabled" yaml:"metrics_enabled"`
}

Config holds the configuration for an object store connection.

func (Config) Validate

func (c Config) Validate() error

Validate returns ErrNilConfig if required fields are missing.

type DriverOpener

type DriverOpener func(cfg Config) (Client, error)

DriverOpener opens a Client for the given Config.

type GetOptions

type GetOptions struct {
	// Range requests a byte range (e.g., "bytes=0-1023"). Empty = full object.
	Range string
}

GetOptions controls object download behavior.

type ListOptions

type ListOptions struct {
	// Prefix filters objects by key prefix.
	Prefix string
	// MaxKeys limits the number of results. 0 = use server default (1000).
	MaxKeys int
	// Recursive lists all objects (not just "folders").
	Recursive bool
}

ListOptions controls object listing behavior.

type ObjectInfo

type ObjectInfo struct {
	Bucket       string
	Key          string
	Size         int64
	ContentType  string
	ETag         string
	LastModified time.Time
	Metadata     map[string]string
}

ObjectInfo holds metadata about a stored object.

type PutOptions

type PutOptions struct {
	ContentType string
	Metadata    map[string]string
	// CacheControl sets the Cache-Control header.
	CacheControl string
}

PutOptions controls object upload behavior.

type ReadCloser

type ReadCloser interface {
	Read(p []byte) (n int, err error)
	Close() error
}

ReadCloser is io.ReadCloser.

type ReadSeeker

type ReadSeeker interface {
	Read(p []byte) (n int, err error)
	Seek(offset int64, whence int) (int64, error)
}

ReadSeeker is io.ReadSeeker (aliased to avoid importing io in the interface).

Directories

Path Synopsis
Package minio registers the "minio" driver for objectstorex.
Package minio registers the "minio" driver for objectstorex.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL