Blob Storage
Val Town now comes with blob storage built-in. It allows for storing any data: text, JSON, images. Access it via std/blob
.
Blob storage is scoped globally to your account. If you set a blob in one val, you can retrieve it by the same key in another val. It’s backed by Cloudflare R2. Blob-stored data counts towards your total Val Town storage - 10mb on the free plan and 1gb on pro. Check our pricing page to learn more.
Usage
JSON
These functions are the ones most commonly used for storing JSON data, including simple strings.
async getJSON(key: string)
- Retrieves a blob as JSON for a given key. Returnsundefined
if the key is not found.async setJSON(key: string, value: any)
- Sets a blob for a given key with a value that is automatically converted to JSON.
List keys
async list(prefix?: string)
: List your blob keys. optionally provide a prefix to filter the query.
Copy key
async copy(previous: string, next: string)
: Copy a blob from one key to another.
Move key
async move(previous: string, next: string)
: Move a blob from one key to another.
Delete keys
async delete(key: string)
: Delete the specified key.
Lower-level get/set
We do provide access to the lower-level getter and setters, which are useful if you are storing non-JSON or binary data, need to stream in your response or request data, or do anything else lower-leve.
async get(key: string)
: Retrieves a blob for a given key.async set(key: string, value: string | BodyInit)
: Sets the blob value for a given key. See BodyInit.
Examples
- https://www.val.town/v/stevekrouse/blobDemo
- https://www.val.town/v/andreterron/exampleSetBlobJson
- https://www.val.town/v/andreterron/exampleGetBlobJson
- https://www.val.town/v/std/docsBlobCounterDemo
- https://www.val.town/v/andreterron/blobSavePictureExample
- https://www.val.town/v/andreterron/blobReadPictureExample
Error Handling
blob.get
can throwValTownBlobNotFoundError
- Any method can throw
ValTownBlobError
for unexpected errors.