{"catalogRevision":"cat_f13ab9a494120b5e","recipeId":"page:guides:next-js:private-file-access","recipeRevision":"r1","canonicalPath":"/guides/next-js/private-file-access","label":"Enforce private-file authorization for Next.js","family":"Framework operation","summary":"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.","representativeQueries":["Enforce private-file authorization for Next.js","Where can I host Enforce private-file authorization built with Next.js?","I need application identity integration, per-file access rules and negative authorization tests."],"breadcrumbIds":["discovery","stack","stack:frameworks","stack:framework-next-js","page:guides:next-js:private-file-access"],"breadcrumbs":[{"id":"discovery","label":"Agent hosting discovery","canonicalPath":"/discover/discovery","kind":"root"},{"id":"stack","label":"Frameworks and execution targets","canonicalPath":"/discover/stack","kind":"facet"},{"id":"stack:frameworks","label":"Frameworks","canonicalPath":"/discover/stack/frameworks","kind":"group"},{"id":"stack:framework-next-js","label":"Next.js","canonicalPath":"/discover/stack/framework-next-js","kind":"atom"},{"id":"page:guides:next-js:private-file-access","label":"Enforce private-file authorization for Next.js","canonicalPath":"/guides/next-js/private-file-access","kind":"recipe"}],"parentIds":["stack:framework-next-js","intent:connect-app-to-storage","pattern:private-document-library"],"parents":[{"id":"stack:framework-next-js","label":"Next.js","canonicalPath":"/discover/stack/framework-next-js","kind":"atom"},{"id":"intent:connect-app-to-storage","label":"Connect app to storage","canonicalPath":"/discover/intent/connect-app-to-storage","kind":"atom"},{"id":"pattern:private-document-library","label":"Private document library","canonicalPath":"/discover/pattern/private-document-library","kind":"atom"}],"resourceRequirements":["primitive:compute","primitive:postgres","primitive:s3-compatible-object-storage"],"infrastructureRequirements":[{"primitiveId":"primitive:compute","label":"Compute","canonicalPath":"/discover/primitive/compute","status":"verified","summary":"Apps run in isolated x86_64 Firecracker microVMs that auto-pause when idle and wake on request; sizes are the priced VM sizes."},{"primitiveId":"primitive:postgres","label":"Postgres","canonicalPath":"/discover/primitive/postgres","status":"verified","summary":"Managed PostgreSQL 16 runs in its own microVM and is auto-provisioned when an app needs a database and no DATABASE_URL is supplied."},{"primitiveId":"primitive:s3-compatible-object-storage","label":"S3-compatible object storage","canonicalPath":"/discover/primitive/s3-compatible-object-storage","status":"verified","summary":"Buckets are S3-compatible with issued credentials; PutObject and GetObject are verified by canary. Other S3 operations are not verified."}],"framework":"Next.js","workload":"Enforce private-file authorization","releaseStatus":"published","supportStatus":"verified","executionStatus":"ready","docsOnly":false,"prerequisites":["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"],"testedConfiguration":{"template":"node-22","runtime":"node","size":"s-1vcpu-1gb","install":"npm install","build":"npm run build --if-present","start":"npm run start"},"inputSchema":{"additionalProperties":false,"properties":{"env":{"description":"Encrypted environment variables: S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY from `ample bucket credentials` (and S3_PUBLIC_URL for a published bucket)","items":{"pattern":"^[A-Z][A-Z0-9_]*=.*$","type":"string"},"maxItems":50,"type":"array"},"name":{"description":"App name (lowercase, digits and dashes)","maxLength":63,"minLength":1,"pattern":"^[a-z0-9-]+$","type":"string"},"path":{"description":"Project directory to deploy, or one service name from ample.toml","maxLength":512,"minLength":1,"type":"string"},"size":{"description":"VM size; omit to let Ample pick a runtime-safe size","enum":["s-1vcpu-256mb","s-1vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb"],"type":"string"}},"required":["env","name","path"],"type":"object"},"outputSchema":null,"workflowSteps":[{"title":"Authorize before touching storage","body":"Return 401 for unauthenticated requests; never expose object keys or the bucket endpoint to the browser."},{"title":"Record metadata in PostgreSQL","body":"Keep the owner, name and object key in a table so authorization decisions come from your data, not from the bucket."},{"title":"Store and stream through the app","body":"PutObject on upload, GetObject on download, both server-side with the injected credentials.","command":"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=..."},{"title":"Test both paths","body":"An unauthenticated request must return 401; an authorized request must return the stored content."},{"title":"Verify","body":"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.","command":"ample logs <deployment_id> --kind build"}],"examples":[{"title":"Next.js private files canary","description":"401 without a token; private=ok with the token after a database write and a bucket round-trip.","sourceRef":"tests/deploy-canaries/next-private-files"}],"successChecks":[{"description":"unauthenticated request is rejected","kind":"http_get","path":"/files/demo","expect":"unauthorized"},{"description":"authorized request returns the private object","kind":"http_get","path":"/files/demo?token=<token>","expect":"private=ok"}],"limitations":["Verified on the node-22 template at s-1vcpu-1gb; other templates and sizes are not verified by this recipe.","Region, compliance attestations and request-duration limits are unknown and not claimed.","Apps auto-pause when idle and wake on the next request; always-on is an operator setting, not a plan feature.","PutObject and GetObject with path-style addressing are verified; multipart upload, listing, presigned URLs and lifecycle rules are not verified.","Bucket credentials are passed as encrypted environment variables; the catalog never creates the bucket for you (use `ample bucket create`).","Storage is allocation-priced per bucket quota and capped by the account plan; the estimate below covers compute only.","Authorization is application code; Ample does not provide managed end-user authentication.","Streaming large files through the app is not verified; the fixture uses small text objects."],"costEstimate":{"currency":"USD","monthlyAmount":10.0,"authoritative":true,"basis":"size prices from pricing.toml (loaded by the API) at build revision c580c64e344ac6391cd4873bd47103b1c5701b48","components":[{"name":"app server","size":"s-1vcpu-1gb","quantity":1.0,"monthlyAmount":5.0},{"name":"managed PostgreSQL database","size":"s-1vcpu-1gb","quantity":1.0,"monthlyAmount":5.0}],"note":"Apps and managed databases auto-pause when idle; the estimate is the always-on monthly price of the tested sizes. Plan quotas and budgets apply."},"evidenceSummary":[{"kind":"canary_run","summary":"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).","observedAt":"2026-09-20T01:13:26Z","implementationRevision":"cc3d82057f1d5e9aa31f682cee71aabfbbbcaabe (CLI e3181f5)","expiresAt":"2027-03-19T01:13:26Z","scope":{"cliVersion":"0.1.20","platform":"hosted","template":"node-22"}},{"kind":"canary_run","summary":"ample bucket create issued credentials; PutObject and GetObject succeeded through the public S3 endpoint; publish exposed the object through the CDN host with an ETag; unpublish and delete cleaned up.","observedAt":"2026-09-20T01:13:26Z","implementationRevision":"cc3d82057f1d5e9aa31f682cee71aabfbbbcaabe (CLI e3181f5)","expiresAt":"2027-03-19T01:13:26Z","scope":{"cliVersion":"0.1.20","platform":"hosted","template":"node-22"}}],"lastVerifiedAt":"2026-09-20T01:13:26Z","mcpBinding":{"registryRef":"mcp:ample_deploy","toolName":"ample_deploy","schemaHash":"876465fce906da0c224d62961d3343064108ebe30af80cf1b87094974e948853","observedAt":"2026-09-20T01:55:48.667900+00:00","implementationRevision":"199ff1dfd526","requiredScopes":["servers:write","databases:read","buckets:read"],"bindingState":"current"},"unknowns":["region availability is unknown until a verified region fact is recorded","compliance attestations are unknown; none are claimed"],"formats":{"html":"https://ample.computer/guides/next-js/private-file-access","markdown":"https://ample.computer/guides/next-js/private-file-access.md","json":"https://api.ample.computer/v1/catalog/recipes/page%3Aguides%3Anext-js%3Aprivate-file-access"},"nextActions":[{"actionId":"browse-catalog","label":"Browse the catalog index","operationId":"catalog_index","method":"GET","relativePath":"/v1/catalog","origin":"api","parameters":{},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"search-recipes","label":"Search published recipes by intent, stack and constraints","operationId":"search_recipes","method":"POST","relativePath":"/v1/catalog/search","origin":"api","parameters":{"body":{"limit":5,"query":"Enforce private-file authorization for Next.js"}},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"plan:page:guides:next-js:private-file-access","label":"Prepare a side-effect-free deployment plan for an authorized project","operationId":"plan_deployment","method":"POST","relativePath":"/v1/catalog/plan","origin":"api","parameters":{"body":{"inputs":{},"projectId":"<workspace or server id you own>","recipeId":"page:guides:next-js:private-file-access","recipeRevision":"r1"}},"requiresAuthentication":true,"requiresApproval":false},{"actionId":"auth-setup","label":"Read the existing agent authentication setup","operationId":"existing_auth_setup","method":"GET","relativePath":"/mcp/setup","origin":"api","parameters":{},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:stack:framework-next-js","label":"Browse Next.js","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/stack%3Aframework-next-js","origin":"api","parameters":{"nodeId":"stack:framework-next-js"},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:intent:connect-app-to-storage","label":"Browse Connect app to storage","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/intent%3Aconnect-app-to-storage","origin":"api","parameters":{"nodeId":"intent:connect-app-to-storage"},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:pattern:private-document-library","label":"Browse Private document library","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/pattern%3Aprivate-document-library","origin":"api","parameters":{"nodeId":"pattern:private-document-library"},"requiresAuthentication":false,"requiresApproval":false}]}