Skip to content

Latest commit

 

History

History
141 lines (103 loc) · 3.01 KB

README.md

File metadata and controls

141 lines (103 loc) · 3.01 KB

Hono Storage

Hono Storage is a storage helper for Hono, this module is like multer in expressjs.

Warning

This is a work in progress. The code is not yet ready for production use.

Installation

npm install @hono-storage/core

Helper

you can use helper to install storage for Hono.

npm install @hono-storage/node-disk # for nodejs disk storage
npm install @hono-storage/memory # for in-memory storage
npm install @hono-storage/s3 # for s3 storage (or r2 storage)

Usage

import { Hono } from "hono";

const app = new Hono();

const storage = // your storage, see below

app.post("/upload/single", storage.single("image"), (c) => c.text("OK"));
app.post("/upload/multiple", storage.multiple("pictures"), (c) => c.text("OK"));
app.post(
  "/upload/field",
  storage.fields({
    image: { type: "single" },
    pictures: { type: "multiple", maxCount: 2 },
  }),
  (c) => c.text("OK"),
);

// and you can get parsed formData easily
app.post("/upload/vars", storage.single("image"), (c) => {
  const { image } = c.var.files;
  // do something with file
  return c.text("OK");
});

// serve app

Storage

Normal Storage
import { HonoStorage } from "@hono-storage/core";

const storage = new HonoStorage({
  storage: (c, files) => {
    // do something with the files, eg, upload to s3, or save to local, etc.
  },
});
Node.js Disk Storage
import { HonoDiskStorage } from "@hono-storage/node-disk";

const storage = new HonoDiskStorage({
  dest: "./uploads",
  filename: (c, file) => `${file.originalname}-${new Date().getTime()}.${file.extension}`,
});
In-Memory Storage
import { HonoMemoryStorage } from "@hono-storage/memory";

const storage = new HonoMemoryStorage({
  key: (c, file) => `${file.originalname}-${new Date().getTime()}`,
});
S3 Storage (Also R2)
import { S3Client } from "@aws-sdk/client-s3";
import { HonoS3Storage } from "@hono-storage/s3";

/** if you use S3 */
const client = new S3Client({
  region: "[your-bucket-region]",
  credentials: {
    accessKeyId: AWS_ACCESS_KEY_ID,
    secretAccessKey: AWS_SECRET_ACCESS_KEY,
  },
});

/** if you use R2 */
const client = new S3Client({
  region: "auto",
  endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
  credentials: {
    accessKeyId: ACCESS_KEY_ID,
    secretAccessKey: SECRET_ACCESS_KEY,
  },
});

const storage = new HonoS3Storage({
  key: (_, file) => `${file.originalname}-${new Date().getTime()}.${file.extension}`,
  bucket: "[your-bucket-name]",
  client,
});

You want to find more? Check out the examples!

License

MIT

Contributing

This project is open for contributions. Feel free to open an issue or a pull request!