> This page location: Functions > Templates > Presigned uploads with Object Storage
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Presigned uploads with Object Storage

Create presigned PUT and GET URLs for a Neon Object Storage bucket.

- First Party: maintained by Neon
- Built by: Neon
- Categories: First Party, Storage
- Built with: [Object Storage](https://neon.com/docs/storage/overview.md)

## Install

```bash
neon functions new presigned-uploads
```

## About this template

This Neon Function creates short-lived presigned 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

```bash
npm install -g neon@latest
neon login
neon link
neon 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:

```bash
neon config add bucket uploads
```

You can also add the `buckets` block to the `neon.ts` that `neon functions new` created:

```ts
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](https://neon.com/docs/storage/overview).

Function URLs are public, so the function only answers callers that send a token you choose. Generate one and add it to `.env.local`:

```bash
export UPLOADS_FUNCTION_TOKEN=$(openssl rand -hex 32)
echo "UPLOADS_FUNCTION_TOKEN=$UPLOADS_FUNCTION_TOKEN" >> .env.local
```

| Variable | Description | Required |
| --- | --- | --- |
| `UPLOADS_FUNCTION_TOKEN` | A random token of at least 32 characters, for example from openssl rand -hex 32, that callers send as Authorization: Bearer \<token\>. Neon Function URLs are public, so the function requires it. | 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`:

```bash
neon deploy --env .env.local
```

### Run locally

```bash
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.

Get a presigned upload URL:

```bash
curl -s http://localhost:8787/uploads \
  -H "authorization: Bearer $UPLOADS_FUNCTION_TOKEN" \
  -H 'content-type: application/json' \
  -d '{"filename":"hello.txt","contentType":"text/plain"}'
```

The response is a `201` like this:

```json
{
  "key": "6f0d...-hello.txt",
  "uploadUrl": "https://br-....storage...",
  "method": "PUT",
  "headers": { "content-type": "text/plain" },
  "expiresIn": 300
}
```

Upload a file to the presigned URL:

```bash
curl -X PUT "<uploadUrl>" \
  -H 'content-type: text/plain' \
  --data-binary 'hello from Neon'
```

List the bucket and get a download URL:

```bash
curl -s http://localhost:8787/files \
  -H "authorization: Bearer $UPLOADS_FUNCTION_TOKEN"
curl -s "http://localhost:8787/files/download?key=<key>" \
  -H "authorization: Bearer $UPLOADS_FUNCTION_TOKEN"
```

### Routes

| Route                         | Auth                            | Purpose                                                                                                                                                   |
| ----------------------------- | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GET /`                       | None                            | Returns the route list, the bucket name, the setup steps, and whether the token is set.                                                                   |
| `POST /uploads`               | Bearer `UPLOADS_FUNCTION_TOKEN` | Takes `{ filename, contentType }` and 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 that expires in 1 hour and downloads the object as an attachment named after its key. Returns `404` if the key doesn't exist. |

If `UPLOADS_FUNCTION_TOKEN` is unset or shorter than 32 characters, the function 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

```bash
neon deploy --env .env.local
neon 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.

## Related

- Previous: [Mastra agent with Postgres memory](https://neon.com/functions/templates/mastra.md)
- Next: [Alt text for uploaded images](https://neon.com/functions/templates/upload-alt-text.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/presigned-uploads)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/presigned-uploads"}` to https://feedback.neon.tech/ (no auth required).