Example in Go
Create an Object Store Bucket
js, _ := nc.JetStream() obj, _ := js.CreateObjectStore(&nats.ObjectStoreConfig{ Bucket: "ASSETS", })
Put an Object
file, _ := os.Open("logo.png") defer file.Close() obj.PutFile("logo", "logo.png")
Get an Object
file, _ := obj.Get("logo") defer file.Close() data, _ := io.ReadAll(file) fmt.Println("Object size:", len(data))
Watch for Updates
watch, _ := obj.Watch() for info := range watch.Updates() { if info != nil { fmt.Printf("Object %s updated, size %d\n", info.Name, info.Size) } }
Use Case Examples
- Static Assets
- Store images, icons, or JSON config files for distributed services.
- Machine Learning Models
- Keep the latest model file in the object store.
- Workers can fetch and reload the model when it changes.
- Backups and Snapshots
- Periodically push database dumps or logs into the object store.
Object Store vs Key-Value Store
Feature | Key-Value Store | Object Store |
---|---|---|
Data size | Small values (configs, flags) | Larger files and binary blobs |
Access | Get/Put by key | Get/Put by object name |
Use case | Feature flags, discovery, coordination | Files, models, assets, backups |
Final Thoughts
The NATS Object Store is a powerful extension of JetStream. It turns NATS into not just a messaging system, but also a distributed storage system. While it’s not meant to replace a full file system or S3, it’s perfect for storing and syncing critical assets inside a NATS-powered architecture.
Pages: 1 2
Category: NATS