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ướipublic/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ặctext/plainvớ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:Responsecủa file. Không có file thì ném lỗiblob not found.getJSON: object đã parse, hoặcundefinedkhi không có file.head: thông tin file, hoặcnull.list: mọi file dướiprefix(tự đi hết các trang).listPage: một trang vàcursorcủa trang sau,nullkhi 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ướipublic/. Key khác thì ném lỗi; với file riêng, dùngpresignGet.set,setJSON,deletekhô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 413BLOB_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.sizetrướ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
keycủa file trên bản ghi.
Cập nhật