Cloudflare
Use Cloudflare Cache, KV, or R2 from Workers, or access KV through the HTTP API.
#Cache API (binding)
Cache data inside Cloudflare Workers with the runtime Cache API.
Driver import: unstorage/drivers/cloudflare-cache-binding
import { createStorage } from "unstorage";
import cloudflareCacheDriver from "unstorage/drivers/cloudflare-cache-binding";
const storage = createStorage({
driver: cloudflareCacheDriver({
base: "my-app",
ttl: 3600,
}),
});Options:
base: Prefixes all cache keys.ttl: Default TTL in seconds.name: Uses a named cache fromcaches.open(name)instead ofcaches.default. Workers for Platforms namespaced scripts require a named cache.
Pass ttl or tag per write to set Cache-Control or Cache-Tag:
await storage.setItem("page:home", "<html>...</html>", {
ttl: 60,
tag: "pages",
});Note
The Cache API cannot list keys. getKeys() returns an empty array, so clearing by base is not supported.
#Cloudflare KV (binding)
Store data in Cloudflare KV and access from worker bindings.
#Usage
Driver name: cloudflare-kv-binding
This driver only works in a Cloudflare Workers environment. Use cloudflare-kv-http in other runtimes.
You need to create and assign a KV. See KV Bindings for more information.
import { createStorage } from "unstorage";
import cloudflareKVBindingDriver from "unstorage/drivers/cloudflare-kv-binding";
export default {
async fetch(_request: Request, env: Env) {
const storage = createStorage({
driver: cloudflareKVBindingDriver({ binding: env.STORAGE }),
});
return Response.json(await storage.getKeys());
},
};Options:
binding: KV namespace binding or a global binding name. Defaults toSTORAGE.base: Prefixes all stored keys.minTTL: Minimum TTL in seconds. Defaults to Cloudflare's minimum of60.
#Cloudflare KV (http)
Store data in Cloudflare KV using the Cloudflare API v4.
#Usage
Driver name: cloudflare-kv-http
You need to create a KV namespace. See KV Bindings for more information.
This driver uses native fetch and works across runtimes. Inside Cloudflare Workers, prefer cloudflare-kv-binding for direct binding access.
import { createStorage } from "unstorage";
import cloudflareKVHTTPDriver from "unstorage/drivers/cloudflare-kv-http";
const storage = createStorage({
driver: cloudflareKVHTTPDriver({
accountId: "my-account-id",
namespaceId: "my-kv-namespace-id",
apiToken: process.env.CLOUDFLARE_API_TOKEN!,
}),
});Options:
accountId: Cloudflare account ID.namespaceId: The ID of the KV namespace to target. Note: be sure to use the namespace's ID, and not the name or binding used in a worker environment.apiToken: API Token generated from the User Profile 'API Tokens' page.email: Email address associated with your account. May be used along withapiKeyto authenticate in place ofapiToken.apiKey: API key generated on the "My Account" page of the Cloudflare console. May be used along withemailto authenticate in place ofapiToken.userServiceKey: A special Cloudflare API key good for a restricted set of endpoints. Always begins with "v1.0-", may vary in length. May be used to authenticate in place ofapiTokenorapiKeyandemail.apiURL: Custom API URL. Defaults tohttps://api.cloudflare.com.base: Prefixes all stored keys.minTTL: Minimum TTL in seconds. Defaults to Cloudflare's minimum of60.
Transaction options:
ttl: Supported forsetItem(key, value, { ttl: number /* seconds min 60 */ })
Supported methods:
getItem:GET /values/:keyhasItem:GET /metadata/:keysetItem:PUT /values/:keyremoveItem:DELETE /values/:keygetKeys:GET /keysclear: Lists keys, then sends chunks of up to 10,000 keys withPOST /bulk/delete.
#Cloudflare R2 (binding)
Store data in Cloudflare R2 buckets and access from worker bindings.
Warning
This experimental driver requires a Cloudflare Workers R2 binding. For other runtimes, use the S3 driver with an R2 S3-compatible endpoint.
#Usage
Driver name: cloudflare-r2-binding
You need to create and assign a R2 bucket. See R2 Bindings for more information.
import { createStorage } from "unstorage";
import cloudflareR2BindingDriver from "unstorage/drivers/cloudflare-r2-binding";
export default {
async fetch(_request: Request, env: Env) {
const storage = createStorage({
driver: cloudflareR2BindingDriver({ binding: env.BUCKET }),
});
return Response.json(await storage.getKeys());
},
};Options:
binding: Bucket binding or name. Default isBUCKET.base: Prefix all keys with base.
Transaction options:
getItemRaw(key, { type: "..." })type: "object": Return the R2 object body.type: "stream": Return body stream.type: "blob": Return aBlob.type: "bytes": Return aUint8Array.type: "arrayBuffer": Return anArrayBuffer(default)
#Cloudflare R2 (http)
To use Cloudflare R2 over HTTP, configure the S3 driver with your R2 endpoint and set region to auto.