Create presigned PUT and GET URLs for a Neon Object Storage bucket.
About this template
This Neon Function hands out short-lived S3 URLs, so a browser or another service can upload and download files without your storage credentials. The files go to a Neon Object Storage bucket, which branches with your database.
Scaffold it
npm install -g neon@latestneon loginneon linkneon functions new presigned-uploads
The CLI writes functions/presigneduploads/index.ts, declares the function in neon.ts, and installs the template's dependencies.
Create the bucket the template expects:
neon config add bucket uploads
You can also add the buckets block to the neon.ts that neon functions new created:
buckets: { uploads: { access: "private" },},
Object Storage is available in aws-us-east-2, aws-us-east-1, aws-eu-central-1, and aws-ap-southeast-1. See Object Storage.
Function URLs are public, so the function only answers callers that send a token you choose. Generate one and add it to .env.local:
Random token (at least 32 characters, for example from openssl rand -hex 32) callers send as Authorization: Bearer <token>; required because Neon Function URLs are public.
Yes
Deploy once so Neon creates the bucket and pulls AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ENDPOINT_URL_S3, and AWS_REGION into .env.local:
neon deploy --env .env.local
Run locally
neon dev
neon dev serves the function at http://localhost:8787/ by default. GET / returns the route list, the setup steps, and whether UPLOADS_FUNCTION_TOKEN is set, and it's the only route that works without the token.
Route list, bucket name, setup steps, and whether the token is set.
POST /uploads
Bearer UPLOADS_FUNCTION_TOKEN
{ filename, contentType } returns 201 with a presigned PUT URL that expires in 5 minutes.
GET /files
Bearer UPLOADS_FUNCTION_TOKEN
Lists up to 100 objects in the bucket.
GET /files/download?key=...
Bearer UPLOADS_FUNCTION_TOKEN
Returns a presigned GET URL with an attachment filename that expires in 1 hour, or 404 if the key doesn't exist.
A missing or shorter-than-32-character UPLOADS_FUNCTION_TOKEN returns 503 with setup instructions, a missing or wrong bearer token returns 401, a malformed body returns 400, and any other path or method returns 404 or 405.
Deploy
neon deploy --env .env.localneon functions get presigneduploads
neon functions get prints the function's public URL. Call it the same way as locally, with the bearer token.
Notes
Keep UPLOADS_FUNCTION_TOKEN on your server. Call this function from your backend after you've checked who the user is, then hand only the presigned uploadUrl or downloadUrl to the browser.
A presigned PUT can't cap the file size. Check size from GET /files after upload, or delete objects you don't want.