Skip to Content

blob

Lưu, đọc, liệt kê và chia sẻ file trong thư mục riêng của app bằng manh/blob.

Cách dùng

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 }); // trình duyệt PUT file thẳng lên đây return { key, uploadUrl, publicUrl: blob.getUrl(key) }; };

Chỉ dùng trong file .ts. Lưu key trên bản ghi, dựng link khi trả về.

Chữ ký

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; };

Tham số

  • key: đường dẫn tương đối trong thư mục của app, không có / ở đầu và không có ... File dưới public/ là file công khai; mọi key khác là file riêng.
  • data (set): nội dung file, tối đa 100 MB mỗi file.
  • opts.contentType (set): kiểu file. Bỏ trống thì lấy từ Blob, hoặc text/plain với chuỗi.
  • prefix (list, listPage): chỉ lấy file có key bắt đầu bằng chuỗi này.
  • opts.cursor, opts.limit (listPage): trang tiếp theo và số file mỗi trang.
  • opts.expiresIn (presignGet, presignPut): số giây link còn dùng được. Mặc định 15 phút, tối đa 7 ngày.

Trả về

  • get: Response của file. Không có file thì ném lỗi blob not found.
  • getJSON: object đã parse, hoặc undefined khi không có file.
  • head: thông tin file, hoặc null.
  • list: mọi file dưới prefix (tự đi hết các trang). listPage: một trang và cursor của trang sau, null khi hết.
  • presignGet, presignPut: link ký sẵn có hạn để tải xuống hoặc tải lên.
  • getUrl: link cố định cho key dưới public/. Key khác thì ném lỗi; với file riêng, dùng presignGet.
  • set, setJSON, delete không trả gì. Mọi hàm ném lỗi khi kho file báo lỗi; file quá 100 MB bị từ chối với lỗi 413 BLOB_TOO_LARGE.

Ví dụ

Tải xuống file riêng qua link có hạn:

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 })); };
  • Mỗi app có thư mục riêng và không đọc được file của app khác. App không giữ thông tin đăng nhập của kho file.
  • Kiểm file.size trước khi tải lên và hiện giới hạn 100 MB trên giao diện.
  • Chủ app xem và quản lý file trong không gian làm việc, ở Dữ liệu › File.

Bước tiếp theo

  • Email, AI, kho file: các dịch vụ có sẵn dùng cùng nhau trong một app.
  • db: lưu key của file trên bản ghi.
Cập nhật