micro/go-micro · error
Failed to get object from bucket
Error message
Failed to get object from bucket
What it means
Returned by getRecord (called from Read) when bucket.Get(key) fails with anything other than nats.ErrKeyNotFound (which is translated to store.ErrNotFound). The key lookup against the JetStream KV bucket failed at the transport or stream level.
Source
Thrown at store/nats-js-kv/nats.go:397
store, err = n.js.CreateKeyValue(kv)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create bucket (%s)", kv.Bucket)
}
}
n.buckets.Set(kv.Bucket, store)
return store, nil
}
// getRecord returns the record with the given key from the nats kv store.
func (n *natsStore) getRecord(bucket nats.KeyValue, key string) (*store.Record, bool, error) {
obj, err := bucket.Get(key)
if errors.Is(err, nats.ErrKeyNotFound) {
return nil, false, store.ErrNotFound
} else if err != nil {
return nil, false, errors.Wrap(err, "Failed to get object from bucket")
}
var kv KeyValueEnvelope
if err := json.Unmarshal(obj.Value(), &kv); err != nil {
return nil, false, errors.Wrap(err, "Failed to unmarshal object")
}
if obj.Operation() != nats.KeyValuePut {
return nil, false, nil
}
return &store.Record{
Key: kv.Key,
Value: kv.Data,
Metadata: kv.Metadata,
}, true, nil
}
View on GitHub (pinned to 24529f1404)
Solutions
- Inspect the wrapped error; if it's a connection/timeout problem, reinitialize the store and retry Read
- Verify the bucket stream is healthy and has a leader ('nats stream info KV_<bucket>')
- Sanitize keys to valid NATS key characters before writing/reading
- Increase JetStream request timeout via nats.JSOpt if timeouts are the cause
Example fix
// before key := rawUserInput // may contain spaces recs, err := st.Read(key) // after key := url.PathEscape(strings.TrimSpace(rawUserInput)) recs, err := st.Read(key)
Defensive patterns
Strategy: retry
Validate before calling
if key == "" || strings.ContainsAny(key, " *>") {
return fmt.Errorf("invalid key %q", key)
} Try / catch
recs, err := st.Read(key)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return nil, err // genuine miss, don't retry
}
if strings.Contains(err.Error(), "Failed to get object from bucket") {
time.Sleep(backoff)
recs, err = st.Read(key)
}
} Prevention
- Distinguish store.ErrNotFound (do not retry) from transport errors (retry)
- Sanitize keys built from user input
- Reconnect on NATS disconnect events before retrying reads
- Set realistic JetStream request timeouts for your network
When it happens
Trigger: Calling store.Read(key) when the connection is broken, the stream has no leader, the request times out, or the key contains invalid characters producing a malformed get subject.
Common situations: Reading immediately after a NATS server restart with a stale connection; reading during cluster failover; keys built from untrusted input containing spaces or wildcards; JetStream request timeouts too aggressive.
Related errors
- source not found: %s
- Failed to store data in bucket '%s'
- Failed to delete data
- Failed to list keys in bucket
- error connecting to nats cluster %v: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/2fcff3d651e90a96.
Report an issue: GitHub.