Enforce private-file authorization for Next.js · Ample Computer

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

You need

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= → “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

What it doesn’t cover