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

Framework operation

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.

supportverifiedreleasepublishedagent runready

VerifiedNovember 6, 2026

What it runs on

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. 1

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

  1. 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.

  1. 3

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

shCopy

$ 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=...
  1. 4

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

  1. 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.

shCopy

$ 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).

  1. 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.