@beaugmented/s3-sandbox

中文文档
0.1.2发布于 2026/10/14MPL-2.0TypeScriptES Module构建来源已验证近 30 天下载 2.3万

A local S3-compatible server for isolated development and tests

原始使用文档
文档目录8 节
  1. Install and run
  2. Programmatic API
  3. Mount in Hono
  4. AWS SDK v3
  5. Supported operations
  6. Bucket configuration and websites
  7. Events
  8. License and attribution

@beaugmented/s3-sandbox

A local S3-compatible server for development and isolated tests, derived from S3rver. Requires Node 26.11.1 and ESM.

This fork is maintained by Augmented. Please report issues and request support in this repository.

Install and run

pnpm add -D @beaugmented/s3-sandbox
pnpm exec s3-sandbox --directory ./s3-data --address 127.0.0.1 --port 4568
pnpm exec s3-sandbox --help

The CLI creates the data directory when needed. --configure-bucket <name> [configs...] preconfigures a bucket with optional XML files and can be repeated. --key and --cert enable TLS. --no-vhost-buckets disables bucket hostnames.

Programmatic API

import { mkdtempSync, rmSync } from "node:fs"
import { tmpdir } from "node:os"
import path from "node:path"
import S3Sandbox from "@beaugmented/s3-sandbox"

const directory = mkdtempSync(path.join(tmpdir(), "s3-test-"))
const sandbox = new S3Sandbox({
    directory,
    address: "127.0.0.1",
    port: 0,
    silent: true,
    configureBuckets: [{ name: "test-bucket" }],
})

try {
    const { port } = await sandbox.run()
    // Use http://127.0.0.1:${port} as the client's endpoint.
} finally {
    await sandbox.close()
    rmSync(directory, { recursive: true, force: true })
}

Use a dedicated existing directory for programmatic instances. Port 0 selects an available port; run() resolves to the listening address. Close the server in test cleanup. Stored objects survive restarts unless resetOnClose is enabled.

OptionDefaultMeaning
directoryOS temporary directory + s3-sandboxFilesystem storage root.
addresslocalhostHost or IP to bind to.
port4568HTTP port; 0 selects an available port.
silentfalseSuppress request logs.
key, cert, pfxunsetNode HTTPS server options.
serviceEndpointamazonaws.comService hostname suffix for virtual hosts.
vhostBucketstrueRecognize bucket names in hostnames.
resetOnClosefalseDelete all bucket data on server close.
allowMismatchedSignaturesfalseAccept well-formed requests with mismatched signatures.
configureBuckets[]Bucket names and optional raw XML strings or buffers.

run(callback) and close(callback) retain the callback forms and return the instance. callback() and its alias getMiddleware() return a handler suitable for http.createServer(). Call configureBuckets() before mounting a handler directly. reset() clears stored bucket data.

The server uses Hono internally and extends Node's EventEmitter. Its public API is the S3 sandbox lifecycle and event API; HTTP framework inheritance and context customization are not supported.

Mount in Hono

getHonoMiddleware() mounts the sandbox in a Hono app served by its Node adapter. Pass the same mount path used in the route, without a trailing slash; omit it when mounting at the root. The adapter supplies the raw Node request needed for S3 signing and streaming uploads.

import { Hono } from "hono"
import { serve, type HttpBindings } from "@hono/node-server"

await sandbox.configureBuckets()
const app = new Hono<{ Bindings: HttpBindings }>()
app.use("/s3/*", sandbox.getHonoMiddleware("/s3"))
const server = serve({
    fetch: app.fetch,
    port: 4568,
    overrideGlobalObjects: false,
})
// Use http://localhost:4568/s3 as the S3 client's endpoint.
// Close the host server during cleanup; sandbox.run() was not called.
await new Promise<void>((resolve, reject) =>
    server.close((error) => (error ? reject(error) : resolve())),
)

AWS SDK v3

Use S3RVER for both signing keys and enable path-style access:

import { S3Client } from "@aws-sdk/client-s3"

const client = new S3Client({
    endpoint: "http://127.0.0.1:4568",
    region: "us-east-1",
    forcePathStyle: true,
    credentials: { accessKeyId: "S3RVER", secretAccessKey: "S3RVER" },
    requestChecksumCalculation: "WHEN_REQUIRED",
    responseChecksumValidation: "WHEN_REQUIRED",
})

The checksum settings select the S3 behavior currently implemented by the sandbox. Current SDK v3 requests and legacy SDK v2 client contracts are tested.

Supported operations

  • Buckets: create, delete, list, object listings with prefixes, delimiters, markers and continuation tokens, location queries, and CORS/website configurations.
  • Objects: PUT, multipart form POST, GET, HEAD, copy, single and batch deletion, tags, metadata, byte ranges, and dummy ACL responses.
  • Multipart uploads: initiate, upload/copy parts, and complete.
  • Authentication: signature versions 2 and 4, presigned URLs, and signed chunked uploads.
  • Websites: index/error documents, redirects, routing rules, and virtual-host requests.

This is a partial S3 implementation intended for local development and tests. Unsupported operations return S3-shaped errors.

Object reads support single byte ranges, including bytes=0-0, open-ended ranges, and suffix ranges such as bytes=-10. Ends and suffix lengths beyond the object size are clamped to the available bytes. Unsatisfiable ranges and ranges against empty objects return 416 InvalidRange; malformed and multiple range headers are ignored.

For multipart POST uploads, submit key, metadata, and response-control fields before the file part. File contents stream to storage even when the part has no filename. The server stores the first file and ignores later form fields.

Bucket configuration and websites

Resolve bundled XML examples through their package exports:

import { readFileSync } from "node:fs"
import S3Sandbox from "@beaugmented/s3-sandbox"

const cors = new URL(
    import.meta.resolve("@beaugmented/s3-sandbox/example/cors.xml"),
)
const website = new URL(
    import.meta.resolve("@beaugmented/s3-sandbox/example/website.xml"),
)
const sandbox = new S3Sandbox({
    directory: "./s3-data",
    configureBuckets: [
        {
            name: "site.local",
            configs: [readFileSync(cors), readFileSync(website)],
        },
    ],
})

Website requests can use a bucket path such as http://localhost:4568/site.local/. To use http://site.local:4568/, resolve site.local to 127.0.0.1 locally. The uploaded index document must match the bucket's website configuration.

See the AWS examples for CORS and website configuration.

Events

Subscribe before performing object operations:

sandbox.on("event", (event) => {
    console.log(event.Records[0].eventName)
})

PUT, POST, COPY, and DELETE operations emit notifications following the S3 event structure.

License and attribution

This fork is licensed under MPL-2.0. Jamie Hall is the original author of S3rver, on which this project is based. NOTICE identifies the inherited material and its original MIT notice. The upstream filesystem suffixes are preserved.

The absorbed multipart implementation also retains Brian White's MIT notice.

版本与发布记录

@beaugmented/s3-sandbox 共发布 4 个版本,首次发布于 2026/10/14;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.2latest2026/10/1424 天前
0.1.12026/10/1424 天前
0.1.02026/10/1424 天前
0.0.0-stage2026/10/1424 天前

关于 @beaugmented/s3-sandbox 的常见问题

@beaugmented/s3-sandbox 是什么?

A local S3-compatible server for isolated development and tests

如何安装 @beaugmented/s3-sandbox?

在项目目录运行 npm install @beaugmented/s3-sandbox,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 s3-sandbox,可以用 npx s3-sandbox 直接运行。

@beaugmented/s3-sandbox 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beaugmented/s3-sandbox 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@beaugmented/s3-sandbox 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 26.11.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@beaugmented/s3-sandbox 可以商用吗?

@beaugmented/s3-sandbox 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@beaugmented/s3-sandbox 的下载量有多少?

根据 npm 官方下载统计,@beaugmented/s3-sandbox 在 2026/10/08 – 2026/11/06 这 30 天内被下载 23,176 次,日均约 773 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beaugmented/s3-sandbox 还在维护吗?

最新版本 0.1.2 发布于 2026/10/14(24 天前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。