blob
Store, read, list and share files in your app’s own folder with manh/blob.
Usage
app/avatars/upload-url/post.ts
import { z } from "@hono/zod-openapi";
import { blob } from "manh/blob";
export const schema = { body: z.object({ type: z.enum(["image/png", "image/jpeg"]) }) };
export default async (c, { user, body }) => {
const key = `public/avatars/${user.id}.${body.type === "image/png" ? "png" : "jpg"}`;
const uploadUrl = await blob.presignPut(key, { expiresIn: 600 }); // the browser PUTs the file straight here
return { key, uploadUrl, publicUrl: blob.getUrl(key) };
};Use it in .ts files only. Store the key on the record and build the link when you return it.
Signature
manh/blob
const blob: {
set(key: string, data: string | Uint8Array | ArrayBuffer | Blob | ReadableStream, opts?: { contentType?: string }): Promise<void>;
get(key: string): Promise<Response>;
getJSON(key: string): Promise<any | undefined>;
setJSON(key: string, value: unknown): Promise<void>;
head(key: string): Promise<{ key: string; size: number; contentType: string; lastModified: string; etag: string } | null>;
list(prefix?: string): Promise<Array<{ key: string; size: number; contentType: string; lastModified: string }>>;
listPage(prefix?: string, opts?: { cursor?: string | null; limit?: number }): Promise<{ objects: Array<{ key: string; size: number; contentType: string; lastModified: string }>; cursor: string | null }>;
delete(key: string): Promise<void>;
presignGet(key: string, opts?: { expiresIn?: number }): Promise<string>;
presignPut(key: string, opts?: { expiresIn?: number }): Promise<string>;
getUrl(key: string): string;
};Parameters
key: a path relative to the app’s folder, with no leading/and no... Files underpublic/are public; every other key is private.data(set): the file content, up to 100 MB per file.opts.contentType(set): the file type. When omitted it comes from theBlob, ortext/plainfor a string.prefix(list,listPage): only files whose key starts with this string.opts.cursor,opts.limit(listPage): the next page and the number of files per page.opts.expiresIn(presignGet,presignPut): how many seconds the link stays valid. Default 15 minutes, at most 7 days.
Returns
get: the file as aResponse. Throwsblob not foundwhen the file is missing.getJSON: the parsed object, orundefinedwhen the file is missing.head: the file’s details, ornull.list: every file underprefix(it walks all pages).listPage: one page and thecursorof the next,nullat the end.presignGet,presignPut: an expiring signed link to download or upload.getUrl: a permanent link for a key underpublic/. Other keys throw; for private files usepresignGet.set,setJSON,deletereturn nothing. Every function throws when file storage reports an error; a file over 100 MB is refused with 413BLOB_TOO_LARGE.
Examples
Download a private file through an expiring link:
app/invoices/[id:int]/download/get.ts
import { blob } from "manh/blob";
import { getInvoice } from "../../repository";
export default async (c, { user, param }) => {
const inv = await getInvoice(user, param.id);
return c.redirect(await blob.presignGet(inv.fileKey, { expiresIn: 3600 }));
};- Each app has its own folder and can’t read other apps’ files. The app holds no storage credentials.
- Check
file.sizebefore uploading and show the 100 MB limit in the UI. - The app owner sees and manages the files in the workspace, under Data › Files.
Next steps
- Email, AI, file storage: the built-in services used together in one app.
- db: store the file’s
keyon the record.
Last updated