s3proxy简单试用
以前简单说过s3proxy 提供了多种模式,支持多种框架,以下是集成website 能力提供静态资源访问的能力
环境准备
- minio
services:
s3:
image: minio/minio:RELEASE.2025-09-07T16-13-09Z
command: server /data --console-address ":9001"
volumes:
- ./s3_data:/data
env_file:
- .env
restart: always
ports:
- "${S3_PORT}:9000"
- "${S3_CONSOLE_PORT}:9001"
参考代码
- 安装依赖
bun add s3proxy express
- index.ts
import express, { type Request, type Response } from 'express';
import { S3Proxy, S3ProxyError } from 's3proxy';
import type { HttpRequest } from 's3proxy';
const app = express();
const proxy = new S3Proxy({
bucket: 'demo',
forcePathStyle: true,
endpoint: 'http://localhost:9000',
region: 'us-east-1',
credentials: {
accessKeyId:"admin",
secretAccessKey: "admin123"
}
});
await proxy.init();
// 提供了静态资源的首页处理
const site = proxy.staticSite({ indexDocument: 'index.html', errorDocument: '404.html' });
// @ts-nocheck until proper types are added for staticSite usage
app.use((req: Request, res: Response) => // @ts-ignore
site(req, res));
app.get('/*splat', async (req: Request, res: Response) => {
try {
const { stream, status, headers } = await proxy.fetch(req as unknown as HttpRequest);
res.writeHead(status, headers);
stream.on('error', () => res.end()).pipe(res);
} catch (err) {
const status = err instanceof S3ProxyError ? err.statusCode : 500;
res.status(status).end();
}
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
- 效果
需要先创建静态资源并同步到s3 中

说明
以上是默认路径的处理,实际上还可以进行改写,参考如下
const site = proxy.staticSite({ indexDocument: 'index.html', errorDocument: '404.html' });
// @ts-nocheck until proper types are added for staticSite usage
app.use("/apps/",(req: Request, res: Response) =>
{
req.url = '/apps' + req.url;
// @ts-ignore
return site(req, res);
});
浙公网安备 33010602011771号