# Add R2 File Uploads to a Cloudflare Worker from a Linear Issue

Yes. An agent reads the labelled Linear issue, adds an R2 binding and attachment upload and download to a Workers app, tests it in workerd and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-r2-file-uploads/cloudflare-workers/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the issue and then the Worker as it is. In a project shaped like the `to-do-list-kv-template` in cloudflare/templates, `wrangler.jsonc` names the Worker, sets `compatibility_date` 2025-10-08 with the `nodejs_compat` flag, serves `./build/client` as static assets, points `main` at `server.ts`, turns observability on and binds one KV namespace as `TO_DO_LIST`. `package.json` runs Remix 2.17 on `@remix-run/cloudflare`, Vite 5.4, Vitest 3.2 through `@cloudflare/vitest-pool-workers` 0.11, `wrangler` 4.136 and Tailwind 3, with `check` running `tsc`, the Remix build and `wrangler deploy --dry-run`, and `cf-typegen` regenerating `worker-configuration.d.ts`, where `Env` currently has the single `TO_DO_LIST: KVNamespace` member. `app/to-do-manager.ts` is a `TodoManager` class over that namespace, storing the whole list as one JSON value under a per-list key with a 300-second `expirationTtl`, and `app/routes/$id.tsx` is the one real route: its `loader` lists todos and its `action` switches on an `intent` form field between `create`, `toggle` and `delete`. `test/to-do-manager.test.ts` exercises the manager against `env.TO_DO_LIST` from `cloudflare:test` with `singleWorker: true` in `vitest.config.ts`.

The issue asks for a file attached to a todo, so the agent adds an R2 bucket rather than stuffing bytes into KV. It adds an `r2_buckets` entry with a binding named `ATTACHMENTS` to `wrangler.jsonc`, runs `npm run cf-typegen` so `Env` gains `ATTACHMENTS: R2Bucket`, and extends the `Todo` interface in `app/to-do-manager.ts` with an optional `attachment` holding the object key, the original filename, the content type and the size. It adds an `app/attachments.ts` module with `put(listId, todoId, file)`, which writes the file's stream to the bucket under `<listId>/<todoId>/<uuid>` with `httpMetadata.contentType` set from the upload, and `get(key)` and `delete(key)` around the bucket API. `TodoManager` gets `attach(id, attachment)` and `detach(id)` that update the stored JSON, and `delete(id)` removes the object when a todo with an attachment is deleted so the bucket does not collect orphans.

In `app/routes/$id.tsx` the form becomes `multipart/form-data` with an optional `file` input beside the text, the `create` intent reads `formData.get("file")`, refuses anything over a size limit the agent names in the pull request with a 413, and calls `put` before storing the todo; a new `attach` intent does the same for an existing todo. A new resource route `app/routes/$id.$todoId.file.tsx` streams the object back with its stored content type and a `Content-Disposition` built from the original filename, after checking the todo in that list actually points at the key, so one list cannot read another list's files. The list UI shows the filename as a link to that route.

Tests stay in the Workers pool. The agent adds `test/attachments.test.ts`, which uses `env.ATTACHMENTS` from `cloudflare:test` to put a small file, read it back with its content type, and confirm `delete` on the todo removes the object, and extends `test/to-do-manager.test.ts` for `attach` and `detach`. It runs `npm run typecheck`, `npm run lint`, `npm test` and `npm run check`, and only when all four pass does it push a branch and open a pull request that links the Linear issue.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled agent says: \"Let a user attach one file to a todo. Store the bytes in R2, keep the filename and content type on the todo, serve the file back from the Worker, and delete the object when the todo is deleted.\" Do the work in this repository: add an r2_buckets binding named ATTACHMENTS to wrangler.jsonc and regenerate the types, add an attachments module and the attach/detach methods on TodoManager, handle a multipart file field in the route action with a size limit, add a resource route that streams the file with its content type, write Vitest cases that run in the Workers pool against the bucket, run typecheck, lint, test and check, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. The diff touches `wrangler.jsonc` for the bucket binding, `worker-configuration.d.ts` as regenerated, `app/to-do-manager.ts` for the `attachment` field and the two new methods, a new `app/attachments.ts`, `app/routes/$id.tsx` for the multipart form and the `attach` intent, a new `app/routes/$id.$todoId.file.tsx`, and the two test files. The description quotes the issue, states the size limit and the object key layout, notes that the bucket must exist before `wrangler deploy` and links the Linear issue.

The proof is the test run and the dry-run deploy. The trajectory at `GET /o/{org}/sessions/{id}/messages` keeps each `npm test` and `npm run check` invocation with its output, so a reviewer sees the attachment cases pass inside workerd and `wrangler deploy --dry-run` accept the new binding without checking the branch out.

Before merging, a reviewer checks that the download route verifies the todo belongs to the list in the URL before touching the bucket, that the size check reads the stream rather than trusting a header, and that deleting a todo removes its object. They also create the R2 bucket in their account, since the agent cannot and the pull request says so.

## Run it on every Linear issue labelled agent

Sign in to Linear once with `POST /me/connections {"type": "app", "name": "linear"}` and finish the `authorize_url` in the console. Add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled`, filtered to the `agent` label; labelling an issue appends it after the prompt and starts a run in the Project's repository, and the pull request links back to the issue. An automation run has its own `max_runs_per_hour` cap and is not tied to an API key.

From your own server, send `POST /o/{org}/sessions` with the repository in `resources` and the issue URL beside it, `https://linear.app/<workspace>/issue/ENG-42`, which puts the issue text in the prompt through your Linear connection. The Rush GitHub App must be installed on the repository, which is the one step the API does not do.

## Why run it on Rush?

Adding a storage binding to a Worker is a config change, a type regeneration and a test run inside workerd, and the mistakes are quiet: a binding name that does not match `Env`, a stream read twice, a key that lets one list read another's files. On Rush the run happens in its own cloud sandbox with its own clone: it runs `wrangler types`, the Vitest Workers pool and the dry-run deploy itself, and your laptop keeps its own `wrangler` login untouched.

The result is a pull request with evidence rather than a description of one. The diff shows every file, the Linear issue is linked, and the trajectory keeps the test output and the dry-run deploy, so review is about the size limit and the key layout, not about whether the tests ran.

## Frequently asked questions

### Does the run create the R2 bucket?

No. Creating a bucket needs your Cloudflare account, and the sandbox has no credentials for it. The pull request adds the `r2_buckets` binding and names the bucket; the tests run against the in-memory bucket the Workers Vitest pool provides, and you create the real one before deploying.

### Why R2 rather than KV for the file?

The template stores each list as one JSON value in KV with a five-minute TTL, which suits small text and not file bytes. R2 holds objects with their own content type and streams them back, so the Worker never buffers a whole file in memory, and deleting a todo can delete its object.

### Is there a size limit?

Yes, the action refuses a file above a limit with a 413 before writing to the bucket, and the pull request states the number it chose so you can change it in review. The check reads the stream rather than a client-supplied header.

### Can one list download another list's file?

No. The download route loads the todo from the list named in the URL and streams only the object key stored on that todo, so a guessed key under another list is a 404. The reviewer's checklist in the pull request includes this check.

### Does it work with Remix's Vite dev server?

Yes. The template already uses Remix's Cloudflare dev proxy to expose bindings in `remix vite:dev`, and the new `ATTACHMENTS` binding reaches the loader and action the same way `TO_DO_LIST` does. Locally, `wrangler` keeps the bucket's objects on disk under `.wrangler`.

## More Cloudflare Workers jobs

- [Add Rate Limiting to a Cloudflare Worker](https://getrush.ai/run/add-rate-limiting/cloudflare-workers/)
- [Add Request ID Logging to a Cloudflare Worker](https://getrush.ai/run/add-request-id-logging/cloudflare-workers/)
- [Add a JSON REST API Over D1 to a Cloudflare Worker](https://getrush.ai/run/add-rest-api/cloudflare-workers/)

## More jobs like this: Turn a Linear label into a PR

- [Add Rate Limiting to an AdonisJS API](https://getrush.ai/run/add-rate-limiting/adonisjs/)
- [Add Rate Limiting to an ASP.NET Core API](https://getrush.ai/run/add-rate-limiting/aspnet-core/)
- [Add Rate Limiting to better-auth Sign-In](https://getrush.ai/run/add-rate-limiting/better-auth/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
