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
- Variables
- func IsDriverRegistered(name string) bool
- func NormalizeError(err error) error
- func RegisterDriver(name string, fn DriverOpener)
- func RegisteredDrivers() []string
- type Client
- type CompletedPart
- type Config
- type DriverOpener
- type GetOptions
- type ListOptions
- type ObjectInfo
- type PutOptions
- type ReadCloser
- type ReadSeeker
Constants ¶
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 ¶
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 ¶
IsDriverRegistered returns true if the named driver has been registered.
func NormalizeError ¶ added in v0.0.5
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.
type CompletedPart ¶
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.
type DriverOpener ¶
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 ¶
ReadCloser is io.ReadCloser.