# Enforce private-file authorization for Next.js

Enforce private-file authorization in a Next.js app on Ample. The route handler authorizes the request first, records file metadata in a managed PostgreSQL database, stores bytes in a private bucket and streams them back only to authorized callers. The bucket is never published, so objects are reachable only through the app.

## What it runs on

- [Computeverified](/content/discover/primitive/compute/index.html)  
  Apps run in isolated x86\_64 Firecracker microVMs; sizes are the priced VM sizes.

- [Postgresverified](/content/discover/primitive/postgres/index.html)  
  Managed PostgreSQL 16 with pgvector runs in its own microVM and is auto-provisioned when an app needs a database and no DATABASE\_URL is supplied.

- [S3-compatible object storageverified](/content/discover/primitive/s3-compatible-object-storage/index.html)  
  Buckets speak the S3 API with SigV4 credentials Ample Computer issues; `ample bucket upload-url` returns a presigned upload URL. PutObject and GetObject are checked by canary.

## You need

- An authorization check in the route handler (session, token or header) before any storage access
- pg reading DATABASE\_URL and @aws-sdk/client-s3 reading the S3\_* variables
- A private bucket (never `ample bucket publish`) and an auto-provisioned or supplied PostgreSQL database
- An Ample account token with servers:write, databases:read and buckets:read

## Exact tested configuration

buildnpm run build --if-present
    installnpm install
    runtimenode
    sizes-1vcpu-1gb
    startnpm run start
    templatenode-22

## Steps

1. **Authorize before touching storage.** Return 401 for unauthenticated requests; never expose object keys or the bucket endpoint to the browser.

2. **Record metadata in PostgreSQL.** Keep the owner, name and object key in a table so authorization decisions come from your data, not from the bucket.

3. **Store and stream through the app.** PutObject on upload, GetObject on download, both server-side with the injected credentials.

```
    $ ample deploy . --name <app-name> --public --env S3_ENDPOINT=... --env S3_REGION=... --env S3_BUCKET=... --env S3_ACCESS_KEY_ID=... --env S3_SECRET_ACCESS_KEY=...
    ```

4. **Test both paths.** An unauthenticated request must return 401; an authorized request must return the stored content.

5. **Verify.** Fetch the live URL and run the success checks below. On failure read the build log, then the runtime log, fix the cause and deploy again; do not blind-retry.

```
    $ ample logs <deployment_id> --kind build
    ```

## How it was checked

✓ unauthenticated request is rejected· http get/files/demo → “unauthorized”  
✓ authorized request returns the private object· http get/files/demo?token=<token> → “private=ok”

## Evidence

1. November 6, 2026 · canary run  
   Next.js route handler that rejects unauthenticated requests with 401, records file metadata in an auto-provisioned managed PostgreSQL database, stores the bytes in a private bucket and streams them back only after authorization (private=ok).

2. November 5, 2026 · canary run  
   ample bucket create issued credentials; PutObject and GetObject succeeded through the public S3 endpoint; publish exposed the object at its public URL with an ETag; unpublish and delete cleaned up.

## Tested examples

- **Next.js private files canary.** 401 without a token; private=ok with the token after a database write and a bucket round-trip.

## What it doesn’t cover

- Verified on the node-22 template at s-1vcpu-1gb.
- Buckets speak the standard S3 API (PutObject and GetObject with path-style addressing are checked by the fixtures); `ample bucket upload-url` returns presigned upload URLs.
- Bucket credentials are passed as encrypted environment variables; the catalog never creates the bucket for you (use `ample bucket create`).
- Storage counts what you store, within your plan's allowance; the estimate below covers compute only.
- Authorization lives in the app, with its framework's auth library.
