Manage your storage space at the edge by using the built-in Edge KV API operations.
Constructor
Creates an instance that is associated with the specified namespace in Edge KV.
Definition
const edgeKv = new EdgeKV({ namespace: "ns"});Parameters
Parameter
Description
namespace
NameSpace is the name of the storage space that you created in the console.
get
Reads data from a namespace.
Definition
get(key[, {type: "type"}])Parameters
Parameter
Description
key
The type is string.
type
You can select one of the following:
stream(default): Returns content in the form of ReadableStream.text: Returns a string.json: Converts the stored JSON content into an Object and then returns it.arrayBuffer: Returns binary data.
Return value
A Promise object is returned. You can call await to ensure that the execution of the get method is complete.
If the key does not exist, the Promise object is resolved to
undefined.If the get method fails to be executed due to an exception, the Promise object is rejected with an error.
Sample code
export default { fetch(request) { return handleRequest(request) } } async function handleRequest(request) { try { const edgeKV = new EdgeKV({ namespace: "ns" }); let getType = { type: "text" }; let value = await edgeKV.get("key", getType); if (value === undefined) { return "EdgeKV get: key not found"; } else { return new Response(value); } } catch (e) { return "EdgeKV get error" + e; } }
put
Writes or updates key-value data to a namespace.
Definition
put(key,value)Parameters
Parameter
Description
key
The type is string. The key cannot be empty.
type
The data that you want to store. Its value can be up to 1.8 MB in size. You can select one of the following:
stringReadableStreamArrayBufferResponse
Return value
A Promise object is returned. You can call
awaitto ensure that the execution of the put method is complete.If the put method is successful, the Promise object is resolved to
undefined.If the put method fails to be executed due to an exception, the Promise object is rejected with an error.
Sample code
export default { async fetch(request) { return handleRequest(request); } } async function handleRequest(request) { try { const edgeKV = new EdgeKV({ namespace: "ns" }); let data = await edgeKV.put("put_string", "string_value") // await edgeKV.put("put_stream", new HTMLStream("test_stream", [])); // await edgeKV.put("put_array_buffer", getArrayBuffer()); // await edgeKV.put("put_array_buffer", new Response("test"); if (data === undefined) { return "EdgeKV put success\n"; } else { return "EdgeKV put failed\n"; } } catch (e) { return "EdgeKV put error" + e; } }
delete
Deletes a key and its value from a namespace.
Definition
delete(key)Parameters
Parameter
Description
key
The key that you want to delete. The type is string.
Return value
A Promise object is returned. You can call await to ensure that the execution of the delete method is complete.
If the delete method is successful, the Promise object is resolved to
true.If the delete method fails to be executed, the Promise object is resolved to
false.If an exception occurs when you execute the delete method, the Promise object is rejected with an error.
Sample code
export default { async fetch(request) { return handleRequest(request); } } async function handleRequest() { try { const edgeKV = new EdgeKV({ namespace: "ns" }); let resp = await edgeKV.delete("key"); if (resp) { return "EdgeKV delete success"; } else { return "EdgeKV delete failed"; } } catch (e) { return "EdgeKV delete error" + e; } }