A file attached to a TrueTicket ticket never passes through the TrueTicket API. The browser uploads it directly to S3 with a presigned URL. The API reserves a row before the upload and checks the result after it, and it never handles the bytes. That’s how attachments have worked since January. In July the flow changed in one important way: a file can now be uploaded while the ticket is still being written and gets linked when the ticket is created. That needed a column and a new row-level security policy, and it’s the second half of this post.
Three calls per file
Each attachment takes three requests, and only two of them go to our API.
The first is POST /api/attachments/init with the file’s name, size and content type (and a ticket ID, when there is one). The API checks the size against the configured maximum and the content type against an allowlist, and when a ticket is given it checks that the caller can see it and upload to it. Then it creates the attachment row in the PENDING state. The file name is sanitized first: anything that isn’t a letter, a digit, a dot, an underscore or a hyphen becomes an underscore, so a name can’t carry a path. The row’s storage key is attachments/{orgId}/{attachmentId}. The response carries the attachment ID and a presigned PUT URL that expires after a configured number of seconds.
The second request is the upload itself. The browser PUTs the file to that URL, straight to S3, with a progress bar. The presigned request includes the declared content type, so S3 only accepts the upload if the browser sends the same Content-Type it declared at init.
The third is POST /api/attachments/{id}/complete. The row has to still be PENDING. The API asks S3 for the object’s metadata with a HeadObject call and compares the stored size with the size declared at init. If they match, the row becomes AVAILABLE. If the object is missing or the sizes differ, it becomes FAILED. Downloads work the same way in reverse: the API checks access, refuses anything that isn’t AVAILABLE and hands back a presigned GET URL.
The TrueTicket API runs as a Fargate task with 512 MiB of memory and 256 CPU units, a quarter of a vCPU. Streaming every attachment through it would put file transfers in the same process that answers ticket queries. With presigned URLs, the bytes go from the browser to S3 and the API handles two small JSON calls per file. A large upload costs the API exactly what a small one does.
There’s a catch we ran into in February. A presigned URL is signed with the credentials of whoever generated it, and S3 authorizes the browser’s PUT as that identity. The API never touches the file, but its role still needs permission to write it. Uploads failed until the infrastructure stack gave the ECS task an explicit role, TicketsTaskRole, with s3:PutObject, s3:GetObject, s3:DeleteObject and s3:ListBucket on the attachments bucket.
For local runs there’s an alternative, chosen with attachment.storage.type. The local storage service keeps files on disk and imitates presigned URLs with single-use tokens that the API’s own file controller accepts. The rest of the flow doesn’t know which one it’s talking to.
Attaching before the ticket exists
Until July, init required a ticket ID, and the storage key included it (attachments/{org}/{ticket}/{attachment}). That meant a user writing a new ticket couldn’t attach the screenshot that explains the problem. They had to create the ticket first and then add the file in a second step.
The change came in three steps. On July 8 the ticket_id column on attachment became nullable and init stopped requiring a ticket (or accepting a comment ID). The storage key dropped the ticket segment at the same time. On July 11 the linking logic changed. When a new ticket or comment arrives with attachmentIds, each one is linked to it, and each one has to be AVAILABLE. A file that never finished uploading can’t end up on a ticket. The same day the API-key filter started allowing init and complete, so integrations that open tickets with an API key can attach files too. On July 12 the create-ticket form got a file picker. When the user submits, it uploads the pending files one after another with their progress, then creates the ticket with the IDs it got back. That merged on July 21.
sequenceDiagram
participant B as Browser
participant A as TrueTicket API
participant S as S3
B->>A: init with name, size and type, no ticket
A->>A: Validate, save PENDING row with its organization
A-->>B: Attachment id and presigned PUT URL
B->>S: PUT the file
B->>A: complete
A->>S: HeadObject
A->>A: Sizes match, mark AVAILABLE
B->>A: Create ticket with attachmentIds
A->>A: Link each AVAILABLE attachment to the new ticket
The policy for files without a ticket
Making the column nullable was the easy part. TrueTicket isolates organizations with PostgreSQL row-level security, and the attachment policy used to reach the organization through the ticket. A row was visible only if its ticket belonged to the current organization or had been requested by it. An attachment with no ticket fails that test, so with the old policy a user’s own unlinked upload was invisible to them, and the policy’s WITH CHECK would refuse to write it in the first place.
The July 11 migration adds organization_id to attachment, filled at init from the uploader’s organization, and rewrites the policy with two ways in. A row is visible when its own organization_id matches the current organization, or when its ticket belongs to (or was requested by) the current organization. Unlinked files are reachable through the first condition and linked ones through either.
An upload that’s started and then abandoned leaves something behind. If the user closes the form after the files went up, those attachment rows stay with no ticket and the objects stay in S3. Nothing cleans them up yet. They’re harmless (only their own organization can see them), but they accumulate, and a scheduled job that removes unlinked attachments older than a day or so is the obvious fix.
Deleting is the other loose end. When an attachment is deleted, the API removes the object from storage and then the row. If the storage call fails, it logs the error and deletes the row anyway, on the reasoning that an orphaned object is easier to clean up later than a row pointing at nothing. That’s the right order, but it means the cleanup job has two kinds of leftovers to find.
The stored objects found a second use in July. When TrueTicket’s email channel started carrying attachments in July, it read the same stored objects, with no new upload path for email.