KubFly SDK for Go / bucket
package bucket
import "kubfly.io/sdk/bucket"
Package bucket is a connection to one bucket of an object bucket: objects by key, listed in the order of their keys (the order of their UTF-8 bytes, as S3 lists them) — the kubfly-sdk-bucket module of the Java SDK. The kinds live in their packages and register themselves when linked in: kubfly.io/sdk/bucket/fs (a directory) and kubfly.io/sdk/bucket/s3 (S3 and S3-compatible clouds).
st, err := fs.NewBuilder().WithRoot("/var/kubfly").Build()
err = st.Put(ctx, "events/v1/…/segment.kfe", data)
for obj, err := range st.List(ctx, "events/v1/index/all/direct/2026/11/04/") { … }
st, err := bucket.BuilderOf("KUBFLY", config.Environ()) // KUBFLY_BUCKET_KIND=FS, KUBFLY_BUCKET_ROOT=…
Every failure is an *Error — a key that is not valid, a configuration that is not complete, a failed input or output, a refusal of the bucket; an object that is not there is not a failure.
Constants
const KeysMaxBytes = 1024
KeysMaxBytes is the most bytes of a key, in UTF-8.
Functions
func BuilderStand
func BuilderStand(envPrefix string, env map[string]string) (string, error)
BuilderStand is the prefix of the stand from the environment: <ENV_PREFIX>_BUCKET_PREFIX, checked; "" if it is not set.
func BuilderVariable
func BuilderVariable(envPrefix string, name string) string
BuilderVariable is the full name of a variable: <ENV_PREFIX>_<NAME>, or the name without a prefix.
func Create
func Create(ctx context.Context, b Bucket, key string, data []byte) (bool, error)
Create writes an object only if a bucket has none with the key: the first writer of a key wins, an object is never replaced — the code of a build, written by every instance of it. A Creator does it in one step; another bucket — Find, then Put, which two writers may both pass. True if it wrote the object, false if the bucket had one — as Bucket.create of the Java SDK.
func KeysCheck
func KeysCheck(key string) error
KeysCheck checks a key, the same rules for every bucket: at most 1024 bytes of UTF-8; levels separated by '/', none empty, none "." or ".."; no leading or trailing '/', no backslash, no control character, valid UTF-8 (no lone surrogate).
func KeysCheckPrefix
func KeysCheckPrefix(prefix string) error
KeysCheckPrefix checks a prefix of keys: its complete levels follow the rules of keys; the last level may be partial (…/2026/11/0) or absent (…/2026/11/); "" is the whole bucket.
func KeysCompare
func KeysCompare(a, b string) int
KeysCompare compares two keys in the order of a listing: by their UTF-8 bytes (the order of their code points, as S3 lists them) — the order of Go strings.
func Register
func Register(builder Builder)
Register registers a kind with its settings by default: the package of the kind calls it in its init.
func WithRetried
func WithRetried(ctx context.Context, retried func()) context.Context
WithRetried is a context whose calls of a bucket tell retried of each of their retries, after the metric of the kind: a caller counts the retries of its own calls apart from the other buckets of the kind — the output to a bucket, sdk/output-bucket/output/bucket/segments/retries. As Measures.observed of the Java SDK.
Types
type Bucket
type Bucket interface {
// Builder is the builder this bucket was built by.
Builder() Builder
// Find are the system fields of an object; None if it is not there.
Find(ctx context.Context, key string) (util.Option[Obj], error)
// List are the objects whose keys start with a prefix ("" — the whole bucket), in the order of their keys. Every
// iteration asks the bucket anew; a failure is the last pair of an iteration.
List(ctx context.Context, prefix string) iter.Seq2[Obj, error]
// ListFilter are the objects whose keys start with a prefix and that match a filter, in the order of their keys.
ListFilter(ctx context.Context, prefix string, filter Filter) iter.Seq2[Obj, error]
// Bytes are the bytes of an object with its fields; None if it is not there.
Bytes(ctx context.Context, key string) (util.Option[Stored[[]byte]], error)
// BytesRange is a range of the bytes of an object: fewer than length at its end; an offset beyond the end or a
// negative range is a failure.
BytesRange(ctx context.Context, key string, offset int64, length int) (util.Option[Stored[[]byte]], error)
// Put writes an object, replacing one with the same key: readers see the old object or the new one, never a part.
Put(ctx context.Context, key string, data []byte) error
// PutStream writes an object from streams of a known length, replacing one with the same key. The bucket may ask
// for the bytes more than once — to sign them and send them, to send them again after a failure — so every call
// of data gives a new stream of the same bytes; the bucket closes each stream it takes.
PutStream(ctx context.Context, key string, data func() (io.ReadCloser, error), length int64) error
// Time is the time of the bucket by its last response, for the estimate of the difference of the clocks; None
// before a response or for a bucket without a clock.
Time() util.Option[BucketTime]
// Delete removes an object; nothing happens if there is none.
Delete(ctx context.Context, key string) error
// Close closes the connection.
Close() error
}
Bucket is a connection to one bucket. Keys follow KeysCheck. Safe for concurrent use.
type BucketTime
type BucketTime struct {
// Local is the local time when the response came, milliseconds since the epoch.
Local int64
// Bucket is the time the bucket gave in it, milliseconds since the epoch.
Bucket int64
}
BucketTime is the time of a bucket by one of its responses (Bucket.Time): a pair for the estimate of the difference of the clocks. The time of the bucket is that of its Date — whole seconds; the true time of the bucket at the response is within [Bucket, Bucket + 1000).
type Builder
type Builder interface {
// Name is the name of the kind, as in the environment: FS, S3.
Kind() string
// Prefix is the prefix of the stand: several stands share a bucket, each under its own prefix
// (<ENV_PREFIX>_BUCKET_PREFIX); the bucket takes and returns the keys of the stand (NewPrefixedBucket). "" — none.
Prefix() string
// WithPrefix is this builder with the prefix of a stand ("" — none).
WithPrefix(prefix string) Builder
// Env is this kind filled from the environment: the settings <ENV_PREFIX>_BUCKET_<SETTING>.
Env(envPrefix string, env map[string]string) (Builder, error)
// Build checks the settings and connects.
Build() (Bucket, error)
// String is the text form, without secrets.
String() string
}
Builder is a kind of bucket with its settings: an immutable value; WithX gives a new value with a setting, X reads it, and Build checks what is required and connects. Every kind lives in its own package and registers itself when the package is linked in (Register in its init): fs.NewBuilder(), s3.NewBuilder().
st, err := fs.NewBuilder().WithRoot("/var/kubfly").WithPrefix("prod").Build() // prod/events/v1/…
st, err := s3.NewBuilder().WithEndpoint("http://127.0.0.1:9000").WithName("kubfly").WithAccess(a).
WithSecret(s).Build()
builder, err := bucket.BuilderOf("KUBFLY", env) // KUBFLY_BUCKET_KIND=FS, KUBFLY_BUCKET_ROOT=…
Builders of one kind are comparable with ==; their String never shows a secret.
func BuilderOf
func BuilderOf(envPrefix string, env map[string]string) (Builder, error)
BuilderOf is the builder of the environment: the kind from <ENV_PREFIX>_BUCKET_KIND, its settings from <ENV_PREFIX>_BUCKET_<SETTING>.
func BuilderValueOf
func BuilderValueOf(kind string) (Builder, error)
BuilderValueOf is the builder of a kind (FS).
func BuilderValues
func BuilderValues() []Builder
BuilderValues are the kinds linked in, by name, with their settings by default.
type Creator
type Creator interface {
// Create writes an object only if the bucket has none with the key: true if it wrote it, false if there was one.
Create(ctx context.Context, key string, data []byte) (bool, error)
}
Creator is a bucket that writes an object only if it has none with the key, in one step: the buckets of the SDK (S3 If-None-Match: *, a link of a file, a map).
type Error
type Error struct {
// Message tells what failed.
Message string
// Err is the cause, or nil.
Err error
}
Error is the only failure of a Bucket and a Builder: a key that is not valid, a configuration that is not complete, a failed input or output, a refusal of the bucket.
func ErrorOf
func ErrorOf(message string, err error) *Error
ErrorOf is a failure with a cause.
func Errorf
func Errorf(message string) *Error
Errorf is a failure without a cause.
func (*Error) Error
func (e *Error) Error() string
Error is the message, with the cause.
func (*Error) Unwrap
func (e *Error) Unwrap() error
Unwrap is the cause.
type Filter
type Filter func(obj Obj) bool
Filter selects the objects of a listing.
type MeasuredBucket
type MeasuredBucket struct {
// contains filtered or unexported fields
}
MeasuredBucket is a bucket with the metrics of its operations (Measures): every builder wraps its bucket in it. The duration, the failures and the bytes of find, get, range, put and delete; the failures of list as its iteration meets them (its pages are timed by the bucket itself, as it reads them).
func NewMeasuredBucket
func NewMeasuredBucket(kind string, bucket Bucket) *MeasuredBucket
NewMeasuredBucket is a bucket of a kind (fs, s3) with its metrics.
func (*MeasuredBucket) Builder
func (m *MeasuredBucket) Builder() Builder
Builder are the settings of the bucket.
func (*MeasuredBucket) Bytes
func (m *MeasuredBucket) Bytes(ctx context.Context, key string) (util.Option[Stored[[]byte]], error)
Bytes are the bytes of an object, timed.
func (*MeasuredBucket) BytesRange
func (m *MeasuredBucket) BytesRange(ctx context.Context, key string, offset int64, length int) (util.Option[Stored[[]byte]], error)
BytesRange is a range of an object, timed.
func (*MeasuredBucket) Close
func (m *MeasuredBucket) Close() error
Close closes the bucket.
func (*MeasuredBucket) Create
func (m *MeasuredBucket) Create(ctx context.Context, key string, data []byte) (bool, error)
Create writes an object only if there is none with the key, timed as a write.
func (*MeasuredBucket) Delete
func (m *MeasuredBucket) Delete(ctx context.Context, key string) error
Delete removes an object, timed.
func (*MeasuredBucket) Find
func (m *MeasuredBucket) Find(ctx context.Context, key string) (util.Option[Obj], error)
Find are the fields of an object, timed.
func (*MeasuredBucket) List
func (m *MeasuredBucket) List(ctx context.Context, prefix string) iter.Seq2[Obj, error]
List are the objects of a prefix.
func (*MeasuredBucket) ListFilter
func (m *MeasuredBucket) ListFilter(ctx context.Context, prefix string, filter Filter) iter.Seq2[Obj, error]
ListFilter are the objects of a prefix that a filter takes.
func (*MeasuredBucket) Put
func (m *MeasuredBucket) Put(ctx context.Context, key string, data []byte) error
Put writes an object, timed.
func (*MeasuredBucket) PutStream
func (m *MeasuredBucket) PutStream(ctx context.Context, key string, data func() (io.ReadCloser, error), length int64) error
PutStream writes an object from streams, timed.
func (*MeasuredBucket) String
func (m *MeasuredBucket) String() string
String is the text form: that of the bucket.
func (*MeasuredBucket) Time
func (m *MeasuredBucket) Time() util.Option[BucketTime]
Time is the time of the bucket.
type Measures
type Measures struct {
// contains filtered or unexported fields
}
Measures are the metrics of a kind of bucket, per operation: sdk/bucket/client/<kind>/<operation>/duration (ns), …/errors (failures — any is CRITICAL), …/retries (attempts again after a failure that may pass), …/bytes (read or written). Operations: find, list, get, range, put, delete. Only metrics — aggregated in their interval, no record per call — so the collector, which writes through a bucket, never feeds itself; frames are not made. The metrics are defined at the first operation, never while KubFly itself starts.
func MeasuresOf
func MeasuresOf(kind string) *Measures
MeasuresOf are the metrics of a kind of bucket, shared by its buckets.
func (*Measures) Done
func (m *Measures) Done(name string, start time.Time, bytes int64)
Done records an operation done, begun at start, with the bytes it read or wrote.
func (*Measures) Failed
func (m *Measures) Failed(name string, start time.Time)
Failed records an operation failed, begun at start.
func (*Measures) KubFly
func (m *Measures) KubFly() *api.KubFly
KubFly is the KubFly of the kind: sdk/bucket/client/<kind>, for its metrics and its warnings.
func (*Measures) Retried
func (m *Measures) Retried(ctx context.Context, name string)
Retried records an operation tried again, and tells the observer of the context of the call (WithRetried).
type Obj
type Obj struct {
// Key is the key.
Key string
// Size is the size, bytes.
Size int64
// LastModified is the time of the last write.
LastModified time.Time
// ETag is the tag of the content: another content, another tag.
ETag string
// StorageClass is the class of storage (STANDARD; FS for a file system).
StorageClass string
}
Obj are the system fields of an object.
type PrefixedBucket
type PrefixedBucket struct {
// contains filtered or unexported fields
}
PrefixedBucket is a bucket under a prefix of another one: several stands share a bucket, each under its own prefix (KUBFLY_BUCKET_PREFIX=prod — the keys prod/events/v1/…). The keys given and returned are those of the stand, without the prefix; the limits of keys count the prefix, as the bucket sees it.
func NewPrefixedBucket
func NewPrefixedBucket(prefix string, bucket Bucket) (*PrefixedBucket, error)
NewPrefixedBucket is a bucket under a prefix (prod, stands/prod) of another one.
func (*PrefixedBucket) Builder
func (p *PrefixedBucket) Builder() Builder
Builder are the settings of the bucket bucket, with the prefix.
func (*PrefixedBucket) Bytes
func (p *PrefixedBucket) Bytes(ctx context.Context, key string) (util.Option[Stored[[]byte]], error)
Bytes are the bytes of an object of the stand.
func (*PrefixedBucket) BytesRange
func (p *PrefixedBucket) BytesRange(ctx context.Context, key string, offset int64, length int) (util.Option[Stored[[]byte]], error)
BytesRange is a range of the bytes of an object of the stand.
func (*PrefixedBucket) Close
func (p *PrefixedBucket) Close() error
Close closes the bucket bucket.
func (*PrefixedBucket) Create
func (p *PrefixedBucket) Create(ctx context.Context, key string, data []byte) (bool, error)
Create writes an object of the stand only if there is none with the key.
func (*PrefixedBucket) Delete
func (p *PrefixedBucket) Delete(ctx context.Context, key string) error
Delete deletes an object of the stand.
func (*PrefixedBucket) Find
func (p *PrefixedBucket) Find(ctx context.Context, key string) (util.Option[Obj], error)
Find are the fields of an object of the stand.
func (*PrefixedBucket) List
func (p *PrefixedBucket) List(ctx context.Context, prefix string) iter.Seq2[Obj, error]
List are the objects of a prefix of the stand, in the order of keys.
func (*PrefixedBucket) ListFilter
func (p *PrefixedBucket) ListFilter(ctx context.Context, prefix string, filter Filter) iter.Seq2[Obj, error]
ListFilter are the objects of a prefix of the stand that a filter takes; the filter sees the keys of the stand.
func (*PrefixedBucket) Put
func (p *PrefixedBucket) Put(ctx context.Context, key string, data []byte) error
Put writes an object of the stand.
func (*PrefixedBucket) PutStream
func (p *PrefixedBucket) PutStream(ctx context.Context, key string, data func() (io.ReadCloser, error), length int64) error
PutStream writes an object of the stand from streams.
func (*PrefixedBucket) String
func (p *PrefixedBucket) String() string
String is the text form.
func (*PrefixedBucket) Time
func (p *PrefixedBucket) Time() util.Option[BucketTime]
Time is the time of the bucket bucket.
type Stored
type Stored[T any] struct {
// Value is the value.
Value T
// Obj are the system fields of the object.
Obj Obj
// Meta is the metadata of the user (x-amz-meta-* of S3), never nil.
Meta map[string]string
}
Stored is an object that is there: its value with its fields — what a read gives inside util.Some, Stored of the Java SDK.
got, err := st.Bytes(ctx, key)
if stored, ok := got.Get(); ok { use(stored.Value, stored.Obj, stored.Meta) }
func NewStored
func NewStored[T any](value T, obj Obj, meta map[string]string) Stored[T]
NewStored is an object that is there; the metadata is copied.
func (Stored[T]) Key
func (s Stored[T]) Key() string
Key is the key of the object.