Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object-Storage

A lightweight, file-based object storage server in Go, being built into a self-hosted cold-storage tier for observability data (logs, traces, metric snapshots).

It comes with an HTTP API and an aws s3-style command-line client (mb, rb, ls, cp, mv, rm, sync, cat, stat). The storage engine is hardened for the things an archive tier must get right: atomic writes, streaming I/O, integrity checksums, and crash safety. The CLI verifies data end to end, so a corrupt transfer is caught instead of silently stored or restored.

Why this exists

Observability platforms (log stores, metrics backends, trace stores) keep recent data on fast, expensive storage. After a few days that data is rarely queried, but it still has to be kept for debugging, audits, and retention policies. Keeping it all on the hot tier is the biggest driver of observability cost.

This project is the cheap tier behind it:

  • Who it's for: teams that want a simple, self-hosted archive for telemetry and test artifacts without running a full distributed object store.
  • What it optimizes for: correctness first (no partial or corrupt objects), then low operational overhead (a single static binary, plain files on disk).
  • How it reduces cost: moves old data to commodity disk, and storage-cli sync ships only new or changed files. Compression and automatic expiry are planned (see the roadmap).

Architecture

 storage-cli / curl
            |  HTTP
            v
   [ HTTP handlers ]        cmd/server/handler.go
            |
            v
   [ Storage engine ]       cmd/server/service.go
     stream -> checksum -> temp file -> fsync -> rename -> metadata
            |
            v
   [ Local disk ]
     storage/data/       object bytes
     storage/metadata/   one JSON file per object and bucket
     storage/tmp/        in-flight uploads (wiped on startup)

How a write works

  1. The request body is streamed into a temp file in storage/tmp/. MD5 and SHA-256 are computed in the same pass, so memory use stays constant regardless of object size.
  2. The temp file is fsynced, so the bytes are on disk before the object becomes visible.
  3. Under a per-object lock, the temp file is atomically renamed into storage/data/<bucket>/<key>. The parent directory is then fsynced (skipped on Windows, which has no directory fsync).
  4. The metadata JSON is written last, also via temp file and rename. An object exists only once its metadata does, so a crash during upload never leaves a visible partial object.

If anything fails or the process is killed mid-upload, the temp file is discarded and the previous version of the object (if any) is untouched.

How the CLI keeps data safe

  • Upload: the file is streamed with a known length while its SHA-256 is computed; the server's reported SHA-256 must match, or the command fails.
  • Download: data goes to a temp file next to the destination and is renamed into place only after its SHA-256 matches the server's X-Content-SHA256 header. A corrupt or interrupted download never creates or overwrites the destination file.
  • Copy and move: bytes are streamed from source to destination and verified on both sides. mv deletes the source only after the copy is verified, and refuses to move an object onto itself.
  • Sync: compares size first, and only when sizes match compares the local file's MD5 with the server's stored ETag, so a file is transferred only if it actually changed (including same-size edits).

Quick start

make build

This creates build/storage-server and build/storage-cli.

Start the server (port 8080, data in ./storage):

./build/storage-server

Use the CLI

./build/storage-cli mb logs
./build/storage-cli cp app.log logs/app/app.log
./build/storage-cli ls logs/app/
./build/storage-cli sync ./var/log/app logs/app        # only new or changed files
./build/storage-cli cp -r logs/app ./restore           # restore a whole prefix
./build/storage-cli stat logs/app/app.log

Or run the scripted demo: make demo.

HTTP API

Method Endpoint Description Success
PUT /buckets/{name} Create a bucket 201
DELETE /buckets/{name} Delete an empty bucket 204 (409 if not empty)
GET /buckets List buckets 200
PUT /objects/{bucket}/{key} Upload an object (key may contain /) 200 + metadata JSON
GET /objects/{bucket}/{key} Download an object (supports Range) 200 / 206
HEAD /objects/{bucket}/{key} Object headers only 200
DELETE /objects/{bucket}/{key} Delete an object 204
GET /objects/{bucket} List objects in a bucket ([] if empty) 200
GET /health Health check 200

GET and HEAD on an object return Content-Type (as stored), ETag (MD5), and X-Content-SHA256.

Errors: 400 invalid bucket or key name, 404 bucket or object not found, 405 wrong method, 409 bucket not empty, 500 server error.

Examples

curl -X PUT http://localhost:8080/buckets/logs

curl -X PUT -H "Content-Type: application/gzip" \
     --data-binary @app-2026-11-21.log.gz \
     http://localhost:8080/objects/logs/app/2026-11-21.log.gz

curl -I http://localhost:8080/objects/logs/app/2026-11-21.log.gz

# first 1 KB only
curl -H "Range: bytes=0-1023" http://localhost:8080/objects/logs/app/2026-11-21.log.gz -o part.bin

CLI reference

storage-cli [--server URL] [--verbose|-v] [--quiet|-q] COMMAND [ARGS...]

Global flags must come before the command. Default server: http://localhost:8080.

Command Description Example
mb <bucket> Create a bucket storage-cli mb logs
rb <bucket> Remove an empty bucket storage-cli rb logs
ls [-r] [bucket[/prefix]] List buckets, or objects (-r: recursive, flat) storage-cli ls -r logs/app
cp [-r] <src> <dst> Upload, download, or copy between buckets storage-cli cp -r ./dir logs/dir
mv [-r] <src> <dst> Copy, then delete the source (only if the copy verified) storage-cli mv logs/a.log s3://old/a.log
rm [-r] <bucket/key or prefix> Delete an object, or everything under a prefix storage-cli rm -r logs/app
sync <src> <dst> Transfer only new or changed files (size, then MD5) storage-cli sync ./logs logs/app
cat <bucket/key> Print an object (SHA-256 verified) storage-cli cat logs/a.log
stat <bucket/key> Show object headers, including SHA-256 storage-cli stat logs/a.log
version, help Version and usage

cp, mv, rm, and sync also accept --dryrun to show what would happen without changing anything.

On-disk format

storage/
  data/<bucket>/<key>              object bytes
  metadata/<bucket>.json           bucket metadata
  metadata/<bucket>/<key>.json     object metadata
  tmp/                             in-flight uploads (cleared at startup)

Object metadata:

{
  "key": "app/2026-11-21.log.gz",
  "size": 1048576,
  "content_type": "application/gzip",
  "etag": "md5-hex",
  "sha256": "sha256-hex",
  "last_modified": "2026-11-21T06:32:10Z"
}

Testing

make test            # or: go test ./...
go test -race ./...  # recommended on Linux/macOS/WSL
make cli-test        # real CLI against a real server (27 checks)
make crash-test      # kill -9 the server mid-upload, restart, verify

Server unit tests cover:

  • Upload/download round trip with SHA-256 verification
  • A failed upload leaves no object and no temp files; a failed overwrite leaves the previous version intact
  • Detection of silent data corruption (flipped bits, same size)
  • 20 concurrent writers to one key never leave data and metadata disagreeing
  • Bucket deletion (refused while non-empty, leaves data intact, name reusable afterwards)
  • Stored Content-Type and X-Content-SHA256 returned on GET and HEAD

CLI unit tests run against an in-memory fake server and cover:

  • Round trips with spaces and # in keys, and empty files
  • A corrupt download is rejected and never touches an existing file or leaves temp files
  • A server-side hash mismatch fails an upload or copy; mv keeps the source when the copy fails
  • mv onto itself is refused; rm -r logs/app never touches logs/app2
  • sync transfers only changes, including same-size content changes
  • Path classification and option parsing

The end-to-end script (scripts/cli_smoke.sh) drives the built CLI against a real server: uploads, recursive copy, sync, move, dry runs, deliberate on-disk corruption, and bucket removal.

The crash test (scripts/crash_test.sh) starts the server, streams a ~500 MB upload at a limited rate, kill -9s the server partway through, restarts it, and checks that the object returns 404 and no temp files remain.

Notes:

  • Run make cli-test and make crash-test on Linux, macOS, or WSL. The scripts use kill, head, dd, and /tmp.
  • Windows has no directory fsync, so on Windows the directory-sync step is skipped. Durability against power loss is only fully covered on Linux and macOS.

Project layout

cmd/
  server/
    server.go        routing and startup
    handler.go       HTTP handlers
    service.go       storage engine (ObjectStorage)
    helper.go        validation, locks, atomic file helpers
    server_test.go   engine unit tests
    bucket_test.go   bucket deletion and handler tests
  cli/
    client.go        entry point, flags, mb/rb/cat/stat, help
    bulk.go          ls, cp, mv, rm, sync
    remote.go        HTTP transfers and integrity checks
    cli_test.go      CLI tests (fake server)
scripts/
  crash_test.sh      crash-injection test
  cli_smoke.sh       end-to-end CLI test
makefile

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages