std
blob
Script
Blob Storage - Docs ↗ Val Town comes with blob storage built-in. It allows for storing any data, like text, JSON, or images. You can 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 Admin Panels Blob Storage in Settings – built-into Val Town - list, download, delete blobs Blob Admin – search, view, edit, upload blobs – built in a val – easy to customize in Val Town! Usage Get JSON import { blob } from "https://esm.town/v/std/blob";
let blobDemo = await blob.getJSON("myKey");
console.log(blobDemo); // returns `undefined` if not found Set JSON import { blob } from "https://esm.town/v/std/blob";
await blob.setJSON("myKey", { hello: "world" }); List keys import { blob } from "https://esm.town/v/std/blob";
let allKeys = await blob.list();
console.log(allKeys);
const appKeys = await blob.list("app_");
console.log(appKeys); // all keys that begin with `app_` Delete by key import { blob } from "https://esm.town/v/std/blob";
await blob.delete("myKey"); Examples Counter RSS Notifications (saving the last run time) Picture: Save & Read Error Handling blob.get can throw ValTownBlobNotFoundError Any method can throw ValTownBlobError for unexpected errors. Utilities Our Blob SDK also includes some utility functions to make working with blobs easier. Copy import { blob } from "https://esm.town/v/std/blob";
await blob.copy("myKey", "myKeyCopy"); Move import { blob } from "https://esm.town/v/std/blob";
await blob.move("myKey", "myKeyNew"); Lower-level API We 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-level. 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 . Limitations 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. Keys for blobs can be up to 512 characters long. 📝 Edit docs
9
postpostscript
expiringBlob
Script
expiringBlob: create-and-forget blobs using UUIDv7 How it Works This tool uses UUIDv7s in blob keys with UNIX timestamps included to keep track of when they expire.
Since UUIDv7s are sortable and the blob list endpoint returns results alphabetically, cleanup (checking the blob list and deleting any expired keys) is efficient since any expired keys will be at the front of the results! Each ID is prefixed with a prefix (default: expireBlob: ) and can be suffixed with a label (parameter order depends on the method).
Example blob keys: expiringBlob:01944432-4837-7a99-94e6-e0e162554e21
^Prefix ^UUIDv7
expiringBlob:01944432-4837-7a99-94e6-e0e162554e21:aaaa
^Prefix ^UUIDv7 ^Label Methods createKey : generate a key you can use with @std/blob methods set or setJSON : generate a blob key and save content to it in one step cleanup : delete expired blobs. You can run this manually or periodically with a cron val . If using a custom prefix, make sure to pass it to this method so that the correct blob list is searched. By default, createKey , set , and setJSON will create keys that expire in an hour, but you can override this by passing the expireAt: Date | number parameter. For convenience, expireIn is provided with methods to create future dates for each of the following: expireIn.hours(2) expireIn.minutes(5) expireIn.seconds(45) expireIn.ms(500) To request data once you've saved it, use @std/blob's get and getJSON as usual with the key provided by set or setJSON ! Full Example import { cleanup, createKey, expireIn, set, setJSON } from "https://esm.town/v/postpostscript/expiringBlob";
import { blob } from "https://esm.town/v/std/blob";
const aaaa = createKey("aaaa"); // equivalent of createKey("aaaa", expireIn.hours(1))
await blob.setJSON(aaaa, {
x: 1,
});
console.log(aaaa, await blob.getJSON(aaaa));
// expiringBlob:01944432-4837-7a99-94e6-e0e162554e21:aaaa { x: 1 }
console.log(await set("bbbb", "test", expireIn.seconds(1)));
// expiringBlob:019443fb-6106-77f2-ab0c-5142b099c81a:bbbb
console.log(await setJSON("cccc", { x: 1 }, expireIn.seconds(1)));
// expiringBlob:019443fb-6327-733c-9a0b-c8b9ee9faef0:cccc
await cleanup();
// [no logs]
await new Promise((resolve) => setTimeout(resolve, 1000));
await cleanup();
// deleting expired blob: expiringBlob:019443fb-6106-77f2-ab0c-5142b099c81a:bbbb
// deleting expired blob: expiringBlob:019443fb-6327-733c-9a0b-c8b9ee9faef0:cccc Forkable Cron Example Create a cron val with the following code to periodically delete expired blobs: import { cleanup } from "https://esm.town/v/postpostscript/expiringBlob";
export default () => cleanup();
0
Updated: January 8, 2025