Kv
The Kv component lets you add a Cloudflare KV storage namespace to
your app.
Minimal example
const storage = new sst.cloudflare.Kv("MyStorage");Link to a worker
You can link KV to a worker.
new sst.cloudflare.Worker("MyWorker", { handler: "./index.ts", link: [storage], url: true});Once linked, you can use the SDK to interact with the bucket.
import { Resource } from "sst";
await Resource.MyStorage.get("someKey");Constructor
new Kv(name, args?, opts?)Parameters
-
namestring -
args?KvArgs -
opts?ComponentResourceOptions
KvArgs
transform?
Type Object
Transform how this component creates its underlying resources.
transform.namespace?
Type WorkersKvNamespaceArgs | (args: WorkersKvNamespaceArgs, opts: ComponentResourceOptions, name: string) => void
Transform the R2 KV namespace resource.
Properties
namespaceId
Type Output<string>
The generated ID of the KV namespace.
nodes
nodes.namespace
Type WorkersKvNamespace
The Cloudflare KV namespace.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource object in the SDK.
-
namespaceIdstringThe generated ID of the KV namespace.
Bindings
When you link a KV storage, the storage will be available to the worker and you can interact with it using its API methods.
import { Resource } from "sst";
await Resource.MyStorage.get("someKey");Methods
static get
Kv.get(name, args, opts?)Parameters
The name of the component.namestring
The arguments to get the KV namespace.argsKvGetArgs-
opts?ComponentResourceOptions
Returns Kv
Reference an existing KV namespace with the given name. This is useful when you create a KV namespace in one stage and want to share it in another.
Imagine you create a KV namespace in the dev stage. And in your personal stage frank,
instead of creating a new namespace, you want to share the same one from dev.
const storage = $app.stage === "frank" ? sst.cloudflare.Kv.get("MyStorage", { namespaceId: "a1b2c3d4e5f6", }) : new sst.cloudflare.Kv("MyStorage");KvGetArgs
namespaceId
Type string
The ID of the existing KV namespace.