Encore buckets and the AWS SDK v3 compared for a Hono API: multipart uploads, presigned URLs, local development and the IAM permissions that go with them

To add S3 uploads to a Hono API on AWS, declare an Encore bucket in the service and call bucket.upload() from the route: encore run stores the files locally, and a deploy creates the S3 bucket in your own AWS account with an IAM role for the service. Encore works alongside Hono, so the routes stay as they are.
The alternative is the AWS SDK v3, where you create the bucket, the IAM policy and a local test setup yourself.
Hono has no storage layer, so an upload route is a multipart parser plus a bucket client. The two decisions are who creates the bucket and whether the bytes pass through your API.
If you are starting a new Hono API, the Encore route removes the most setup. If the bucket already exists in Terraform or CloudFormation, the SDK route is the smaller change.
Encore is a backend framework with built-in infrastructure, and it works alongside the framework you already use. For Hono, that means the app keeps its routes and middleware and Encore supplies the bucket: a Bucket declared in code that runs on local files during encore run and becomes an S3 bucket in your own AWS account on deploy.
Install the CLI and create the app first:
Run in your terminal to get started locally.
Hono is built on Web Standards and Encore's raw endpoints hand you Node's request and response objects, so the Hono app is mounted behind one catch-all endpoint. The handler below is a small adapter we wrote that builds a Web Request from those objects, calls app.fetch and writes the response back. It is not a Hono or Encore API:
// uploads/uploads.ts
import { api } from "encore.dev/api";
import { Bucket } from "encore.dev/storage/objects";
import { Hono } from "hono";
import { bodyLimit } from "hono/body-limit";
// Private bucket: S3 in your AWS account when deployed, local files under `encore run`.
const avatars = new Bucket("avatars", { versioned: false });
const app = new Hono();
app.get("/healthz", (c) => c.json({ ok: true }));
// Multipart upload through the API: fine for small files.
app.post(
"/avatars/:userId",
bodyLimit({ maxSize: 5 * 1024 * 1024, onError: (c) => c.json({ error: "file too large" }, 413) }),
async (c) => {
const form = await c.req.parseBody();
const file = form["file"];
if (!(file instanceof File)) {
return c.json({ error: "send the image as a multipart field named file" }, 400);
}
if (!["image/png", "image/jpeg"].includes(file.type)) {
return c.json({ error: "only PNG and JPEG are accepted" }, 415);
}
const key = `${c.req.param("userId")}.${file.type === "image/png" ? "png" : "jpg"}`;
const attrs = await avatars.upload(key, Buffer.from(await file.arrayBuffer()), {
contentType: file.type,
});
return c.json({ key, size: attrs.size }, 201);
},
);
// Encore passes every request it does not route elsewhere to the Hono app.
export const honoApp = api.raw(
// Raw endpoints accept 2 MiB by default, so raise it to match Hono's 5 MB limit.
{ expose: true, method: "*", path: "/!rest", bodyLimit: 5 * 1024 * 1024 },
async (req, res) => {
const method = req.method ?? "GET";
const headers = new Headers();
for (const [name, value] of Object.entries(req.headers)) {
if (value !== undefined) headers.set(name, Array.isArray(value) ? value.join(", ") : value);
}
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
const hasBody = method !== "GET" && method !== "HEAD";
const response = await app.fetch(
new Request(`http://${req.headers.host ?? "localhost"}${req.url}`, {
method,
headers,
body: hasBody ? Buffer.concat(chunks) : undefined,
}),
);
// Object.fromEntries would merge repeated Set-Cookie headers into one, so pass them as a list.
const responseHeaders: Record<string, string | string[]> = Object.fromEntries(response.headers);
const setCookies = response.headers.getSetCookie();
if (setCookies.length > 0) responseHeaders["set-cookie"] = setCookies;
res.writeHead(response.status, responseHeaders);
res.end(Buffer.from(await response.arrayBuffer()));
},
);
The service needs a one-line uploads/encore.service.ts (export default new Service("uploads")). The adapter reads the whole body into memory before Hono sees it, which is the reason for the 5 MB bodyLimit and for using signed URLs below for anything bigger. A local test with @hono/node-server 2.1.11 under encore run on Node 24.22.1 failed when getRequestListener handled POST bodies behind api.raw, with stream.req.removeListener is not a function, so this is a small adapter we wrote that builds the Request itself.
Run it with encore run and send a file with curl. The type and field checks answer before anything is stored:
On deploy, Encore creates the bucket in S3 on AWS in your own account and provisions an IAM role for the service, which is what the upload call runs as. There is no bucket name or access key in the code.
An agent adding uploads to a Hono API writes the Bucket in the same change as the route and tests it with curl -F against encore run, with no bucket, policy or LocalStack to set up first.
A function that returns a short-lived URL keeps the bytes off your API. The client sends a PUT straight to the bucket and only the key and metadata pass through Hono. Add these routes above the api.raw export:
// Large files: hand the client a short-lived URL and keep the bytes off the API.
app.post("/avatars/:userId/upload-url", async (c) => {
const url = await avatars.signedUploadUrl(`${c.req.param("userId")}.png`, { ttl: 900 });
return c.json({ url: url.url });
});
// Private bucket: redirect to a signed download URL.
app.get("/avatars/:userId", async (c) => {
const key = `${c.req.param("userId")}.png`;
if (!(await avatars.exists(key))) return c.json({ error: "not found" }, 404);
const url = await avatars.signedDownloadUrl(key, { ttl: 300 });
return c.redirect(url.url, 302);
});
Under encore run the first route returns a local signed URL that accepts a PUT with the file as the body, and the second answers with a 302 to a signed download URL, or a 404 for a key that does not exist. Anyone holding a signed upload URL can write that one key until it expires, so keep the ttl short and issue it only after your own auth check. These routes, like the upload route above, trust the userId in the path to keep the example short. In production, put Hono auth middleware in front of them and build the key from the signed-in user, or reject a path userId that does not match it. Browser uploads straight to the URL also need CORS rules on the bucket, which this test did not cover.
The direct route uses @aws-sdk/client-s3 with @hono/node-server. The client reads credentials from the environment: an AWS profile or access keys locally, and the task, instance or function role in production.
// src/app.ts
import { serve } from "@hono/node-server";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { Hono } from "hono";
import { bodyLimit } from "hono/body-limit";
const s3 = new S3Client({ region: process.env.AWS_REGION });
const Bucket = process.env.AVATARS_BUCKET!;
const app = new Hono();
app.post(
"/avatars/:userId",
bodyLimit({ maxSize: 5 * 1024 * 1024, onError: (c) => c.json({ error: "file too large" }, 413) }),
async (c) => {
const form = await c.req.parseBody();
const file = form["file"];
if (!(file instanceof File)) return c.json({ error: "send the image as a multipart field named file" }, 400);
const Key = `${c.req.param("userId")}.png`;
await s3.send(new PutObjectCommand({
Bucket,
Key,
Body: Buffer.from(await file.arrayBuffer()),
ContentType: file.type,
}));
return c.json({ key: Key }, 201);
},
);
app.post("/avatars/:userId/upload-url", async (c) => {
const command = new PutObjectCommand({
Bucket,
Key: `${c.req.param("userId")}.png`,
ContentType: "image/png",
});
// Signing the content-type header is what makes S3 reject a different type.
const url = await getSignedUrl(s3, command, {
expiresIn: 900,
signableHeaders: new Set(["content-type"]),
});
return c.json({ url });
});
serve({ fetch: app.fetch, port: 3000 });
This code was type-checked against @aws-sdk/client-s3 3.1175.0 and Hono 4.13.21 but not run against a real bucket, and the same auth note applies: derive the key from the signed-in user before going to production. Hono's bodyLimit middleware rejects oversized bodies before they are parsed. With a presigned URL, the client must send the same Content-Type that was signed, here image/png, or S3 rejects the request with a signature error. The S3 presigned upload guide covers expiry and the permissions the signing identity needs.
Around that route you still create the bucket and keep Block Public Access on for private files, write an IAM policy that allows s3:PutObject on that bucket's prefix only, attach it to the role the app runs as, add a CORS rule if browsers upload directly, and give every developer and CI job a way to run the route, whether that is a shared dev bucket, LocalStack or MinIO. Each of those is Terraform, CloudFormation or console work that lives outside the route, and a change to the route usually means a change there too.
Pick the Encore bucket if the Hono API is new or the bucket does not exist yet: it ranks first because the bucket, its IAM role and the local test setup come from the same declaration as the route. Use signedUploadUrl() once files pass a few megabytes, and keep the multipart route for small files you want to validate. Pick the AWS SDK when a bucket, policy and CORS rules already exist in your Terraform and you only need the route. The two combine, since an existing Hono app can keep its routes while the new upload service moves to Encore.
For the rest of the Hono ecosystem and what else to pair with it, see Hono alternatives and the Hono vs Encore comparison.
Declare an Encore Bucket in the service, parse the multipart body with c.req.parseBody() in a Hono route and call bucket.upload(key, data, { contentType }). Encore runs the bucket on local files under encore run and creates the S3 bucket in your own AWS account on deploy, with an IAM role for the service. The alternative is the AWS SDK v3: create the bucket and IAM policy yourself, then send a PutObjectCommand from the route.
Small files such as avatars can go through the API, which lets you validate type and size before anything is stored. Large files should go straight to the bucket with a presigned or signed upload URL, so the bytes never pass through your server and only the object key and metadata do.
Yes. The same PutObjectCommand and presigned URL code runs in a Hono handler on Lambda, and the function's execution role needs s3:PutObject on the bucket. Lambda caps the request payload at 6 MB, which is another reason to use presigned URLs for anything larger.
No. You can point the AWS SDK at a real bucket, or run LocalStack or MinIO and set a custom endpoint.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.