Skip to Content

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 under public/ 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 the Blob, or text/plain for 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 a Response. Throws blob not found when the file is missing.
  • getJSON: the parsed object, or undefined when the file is missing.
  • head: the file’s details, or null.
  • list: every file under prefix (it walks all pages). listPage: one page and the cursor of the next, null at the end.
  • presignGet, presignPut: an expiring signed link to download or upload.
  • getUrl: a permanent link for a key under public/. Other keys throw; for private files use presignGet.
  • set, setJSON, delete return nothing. Every function throws when file storage reports an error; a file over 100 MB is refused with 413 BLOB_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.size before 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

Last updated