Thanks to visit codestin.com
Credit goes to github.com

Skip to content

joncrangle/storage-proxy

Repository files navigation

Azure Blob / S3 Bucket Proxy Server

A secure, authenticated proxy service for Azure Blob Storage and AWS S3 buckets, using Microsoft Entra ID for authentication. Tracks file accesses and provides aggregate statistics, with OpenAPI documentation and a Scalar UI for easy exploration.

πŸš€ Features

  • πŸ” Proxy requests to Azure Blob Storage containers and S3 buckets via Hono
  • πŸ” Authenticated access via Microsoft Entra ID (OAuth)
  • πŸ“¦ Tracks accesses to files across containers/buckets
  • πŸ“ˆ View top accessed files collectively and per container/bucket
  • πŸ“Š Retrieve aggregate statistics collectively and per container/bucket
  • πŸ“… Time-range filtering for metrics
  • πŸ§ͺ Built-in validation via Zod
  • πŸ“ Metrics persistence to SQLite (via Drizzle ORM) with retention policy
  • πŸ“„ OpenAPI documentation at /openapi and interactive docs at /docs (Scalar UI)
  • πŸ§‘β€πŸ’» Structured logging via Pino

πŸ–ΌοΈ Architecture Diagram

Retrieving Files

flowchart TD
    A[User] -->|Request file| B[Proxy Server]
    B --> C{Authenticated?}
    C -- No --> D[Reject: 401 Unauthorized]
    C -- Yes --> E[Proxy to Azure / S3 Storage]
    E --> F[Track Access Metrics]
    F --> G[Aggregate Metrics]
    E --> H[Cloud Storage]
    H -- Return file --> A
Loading

Retrieving Metrics

flowchart TD
    A[Admin] -->|Request stats| B[Proxy Server]
    B --> C{Authenticated?}
    C -- No --> D[Reject: 401 Unauthorized]
    C -- Yes --> E[Export Metrics]
    E -- csv or json --> A
Loading

πŸ—‚ Project Structure

.
β”œβ”€β”€ docker/
β”‚   └── Dockerfile                  # Dockerfile for building the application
β”œβ”€β”€ mock/
β”‚   β”œβ”€β”€ aws/
β”‚   β”‚   β”œβ”€β”€ moto.http               # Setup S3 buckets and objects for Moto
β”‚   β”‚   β”œβ”€β”€ prepopulate-moto.ts     # Prepopulate Moto with test data
β”‚   β”‚   └── s3.js                   # Signing helper for `moto.http`
β”‚   β”œβ”€β”€ azure/
β”‚   β”‚   β”œβ”€β”€ azurite.http            # Setup azurite containers
β”‚   β”‚   β”œβ”€β”€ prepopulate-azurite.ts  # Prepopulate Azurite with test data
β”‚   β”‚   └── az.js                   # Signing helper for `azurite.http`
β”‚   β”œβ”€β”€ test.html                   # Test anchor tags
β”‚   └── test.pdf                    # Test file
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ auth.ts                 # Microsoft Entra ID authentication middleware
β”‚   β”‚   β”œβ”€β”€ core.ts                 # Core request handler
β”‚   β”‚   └── logger.ts               # Structured logging middleware
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ files.ts                # File proxy endpoints
β”‚   β”‚   β”œβ”€β”€ metrics.ts              # Metrics API endpoints
β”‚   β”‚   β”œβ”€β”€ index.ts                # Main API router
β”‚   β”‚   └── files.schemas.ts        # Zod schemas for file routes
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ azure.ts                # Azure blob provider
β”‚   β”‚   β”œβ”€β”€ s3.ts                   # S3 bucket provider
β”‚   β”‚   β”œβ”€β”€ db.ts                   # SQLite/Drizzle ORM setup
β”‚   β”‚   β”œβ”€β”€ metrics.ts              # Metrics provider
β”‚   β”‚   β”œβ”€β”€ logger.ts               # Logger provider
β”‚   β”‚   └── storage.ts              # Storage provider interface
β”‚   β”œβ”€β”€ app.ts                      # Main Hono server
β”‚   β”œβ”€β”€ config.ts                   # Environment configuration
β”‚   β”œβ”€β”€ server.ts                   # Bun server entrypoint
β”‚   └── types.ts                    # Type definitions
β”œβ”€β”€ tests/
β”‚   └── app.test.ts                 # Bun test runner tests
β”œβ”€β”€ .sample.env                     # Example environment variables
β”œβ”€β”€ bun.lock                        # Bun lock file
β”œβ”€β”€ docker-compose.yml              # Docker Compose configuration
β”œβ”€β”€ package.json                    # Project dependencies and scripts
β”œβ”€β”€ tsconfig.json                   # TypeScript configuration
└── vitest.config.ts                # (Legacy) Vitest configuration

πŸ”§ Configuration

See .sample.env for environment variables. Copy to .env and fill in your Azure Blob Storage or AWS S3 credentials and app secrets.

πŸ“‘ API Endpoints

  • /v1/files: List, view, and download files from containers/buckets
  • /v1/metrics: Access metrics/statistics about file access, containers, and export metrics as JSON/CSV
  • /health: Health check endpoint
  • /openapi: OpenAPI spec (machine-readable)
  • /docs: Interactive API documentation (Scalar UI)

πŸ—οΈ Local Usage

Build and Serve

bun start

🐳 Docker Usage

Recommended to use docker compose to run the application.

  1. Copy your .env file (with secrets/config) into the project root, or use Docker secrets/volumes as needed.
  2. Run the storage-proxy container:
docker compose up -d

The app will be available at http://localhost:3000 (or the port you map).

πŸ§ͺ Development

bun install
bun run dev

Update database after changing Drizzle schema:

bun run drizzle-kit push

Run Drizzle Studio to manage database:

bun run drizzle-kit studio

The application will be available at http://localhost:3000.

Azurite Blob Storage Emulator

See mock/azure/azurite.http and mock/azure/prepopulate-azurite.ts for helpers to create containers and test blobs in the Azurite emulator.

bun run azurite

Moto AWS S3 Emulator

Moto is an in-memory server that emulates AWS services. uv is required with the current setup.

bun run moto

Then use mock/aws/moto.http and mock/aws/prepopulate-moto.ts to create buckets and upload test files.

Testing

This project uses Bun’s built-in test runner. Run tests with:

bun test

Note

Tests run against the mock services:

  • Azurite (for Azure Blob Storage) and creating test containers/blobs using mock/azure/azurite.http
  • Moto (for S3)
  • All major endpoints and edge cases are covered, including authentication, metrics, file access, and error handling.

About

Authenticated proxy for Azure Blob Storage or AWS S3 Buckets with Entra ID

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 3

  •  
  •  
  •