Create and manage streams
Create buckets and streams from Go, TypeScript, or HTTP, then list them and change their settings.
A bucket groups streams and holds the settings new streams take by default. Create the bucket first, then its streams, either explicitly or on first use. This page creates, lists, and changes both; Trim and delete removes them. Buckets and Streams explain the ideas. The API reference has the stream and bucket routes, and the CLI has nova stream and nova bucket.
Create a bucket
EnsureBucket / ensureBucket creates the bucket, or returns it when it already exists, so it is safe to call at startup. This bucket creates a stream on its first append and keeps records for 7 days by default:
bucket, err := c.EnsureBucket(ctx, client.Bucket{
Name: "demo",
CreateOnAppend: true, // the first append to a missing stream creates it
Defaults: client.StreamOptions{
Retention: client.RetentionPolicy{MaxAge: 7 * 24 * time.Hour},
},
})
if err != nil {
return err
}
days := bucket.Defaults.Retention.MaxAge.Hours() / 24
fmt.Println(bucket.Name, "in", bucket.Location, "keeps records for", days, "days")demo in local keeps records for 7 daysAn existing bucket keeps its settings, so ensuring it again changes nothing. UpdateBucket / updateBucket (PUT /v1/buckets/{bucket}) replaces them. The two flags apply at once, and new defaults apply to streams created afterwards.
Create a stream
CreateStream / createStream creates a stream at its address, a bucket and a key. A setting you leave unset takes the bucket's default; here, the 7-day retention comes from the bucket:
created, err := c.CreateStream(ctx, client.StreamOptions{
Bucket: "demo",
Key: "events",
Class: client.Standard,
Producer: client.ProducerFenced, // one writer at a time
})
if err != nil {
return err
}
days := created.Retention.MaxAge.Hours() / 24
fmt.Println(created.Key, created.Class, created.Producer, days, "days")events standard fenced 7 daysAt creation, a zero value means unset, so it takes the bucket's default. Uncapped timestamping and an age limit cannot be combined. An address that is taken fails, unless the creation token matches.
A bucket can also create a missing stream when it is first appended to or read (create on first use). A tail check, a fence, or a trim needs a stream that exists. GetStream / getStream (GET /v1/streams/{bucket}/{key}) returns a stream's settings and its ID, which names the stream in logs. Requests always use the address.
List buckets and streams
A listing returns one bucket's streams in key order, at most 1,000 a page, and can keep only the keys that start with a prefix. Ask for pages until the cursor comes back empty:
opts := client.ListOptions{KeyPrefix: "events", Limit: 100}
for {
page, next, err := c.ListStreams(ctx, "demo", opts)
if err != nil {
return err
}
for _, s := range page {
fmt.Println(s.Key, s.Class)
}
if len(next) == 0 {
break // the last page
}
opts.After = next
}events standard
events/archive standardListBuckets / listBuckets (GET /v1/buckets) lists buckets the same way, in name order, with a name prefix. It needs a grant to list every bucket the prefix covers, so a token for one bucket cannot list buckets. No listing spans buckets.
Change a stream's settings
ReconfigureStream / reconfigureStream (PATCH) changes only the settings you name. Here a zero means "no limit", which a create cannot say:
noAgeLimit := time.Duration(0)
perSecond := int64(1 << 20)
st, err := c.ReconfigureStream(ctx, stream, client.StreamReconfiguration{
RetentionMaxAge: &noAgeLimit, // keep records for any age
Throughput: &perSecond, // at most 1 MiB a second
})
if err != nil {
return err
}
fmt.Println("max age:", st.Retention.MaxAge.Hours(), "hours")
fmt.Println("throughput:", st.Throughput.MaxBytesPerSecond, "bytes a second")max age: 0 hours
throughput: 1048576 bytes a secondA retention change applies at once, with one exception: a shorter age on a stream that already has an age and no size limit applies only once the oldest kept records pass the old age. Trim to the new cutoff to apply it sooner. A change of class, producer policy, timestamping, or throughput passes the stream's writes through a short handoff: an open producer reconnects once, and every append after the call uses the new settings. A setting you leave out stays as it is. In Go the fields are pointers, so leave it nil. In TypeScript, leave the field out. Settings lists every setting with its default.
When a call is refused
| Refusal: Go, TypeScript, HTTP | When | What to do |
|---|---|---|
AlreadyExists gRPC status, TransportError with code ALREADY_EXISTS, 409 | The bucket name or stream address is taken | Use another name, or retry with the same creation token |
ErrStreamNotFound, StreamNotFoundError, 404 | The bucket or stream does not exist | Create it |
FailedPrecondition gRPC status, TransportError with code FAILED_PRECONDITION, 503 | The node cannot serve the class you asked for, such as Express on a node without Express storage | Ask the operator to set up the class |
ErrLocation, LocationMismatchError | The bucket names a location other than the client's | Create the bucket from a client in its location |
ErrPrefixUnsupported, PrefixUnsupportedError | The server is too old to list by prefix | Upgrade the server |
The SDKs check settings before they send them, and refuse a wrong one with an error that names it, such as initial ranges on a plain stream or uncapped timestamping with an age limit. Handle errors lists every error.