Edit on val.town ↗

Blob storage

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 comes in two flavors: scoped & global.

Scoped blob storage should be your default for new vals: it's scoped to the val, so more secure and flexible. Scoped storage is available behind the https://esm.town/v/std/blob/main.ts export and is backed by Amazon S3.

Global blob storage was the norm before scoped blob storage was introduced: it's available to any val in your account. It's available at https://esm.town/v/std/blob and backed by Cloudflare R2.

The API for scoped blob storage and global storage is the same: to use these examples with global val storage, import from https://esm.town/v/std/blob instead.

import { blob } from "https://esm.town/v/std/blob/main.ts";

let blobDemo = await blob.getJSON("myKey");
console.log(blobDemo); // returns `undefined` if not found

import { blob } from "https://esm.town/v/std/blob/main.ts";

await blob.setJSON("myKey", { hello: "world" });

import { blob } from "https://esm.town/v/std/blob/main.ts";

await blob.set("myKey", "String, stream, etc");

import { blob } from "https://esm.town/v/std/blob/main.ts";

// Exposes a Response object with the same methods as a response from
// fetch: .json(), .text(), etc.
const text = await blob.get("myKey").then((response) => response.text());

import { blob } from "https://esm.town/v/std/blob/main.ts";

let allKeys = await blob.list();
console.log(allKeys);

const appKeys = await blob.list("app_");
console.log(appKeys); // all keys that begin with `app_`

import { blob } from "https://esm.town/v/std/blob/main.ts";

await blob.delete("myKey");

We also include copy() and move() couple utility functions to make working with blobs easier.

import { blob } from "https://esm.town/v/std/blob/main.ts";

await blob.copy("myKey", "myKeyCopy");
await blob.move("myKey", "myKeyNew");

For scoped blob storage, there's a Blob Storage option in the sidebar of vals you own, that allows you to list, create, delete, and download blobs.

For global blog storage, there is a blob storage settings where you can list, download, and delete blobs. You can also create your own blob storage interface in userspace, like this blob admin val to search, view, edit, and upload blobs.

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.

Last updated