The Problem: Fragile Resume URLs in Upload CLIs
Building a resilient Command Line Interface (CLI) for file uploads presents a common challenge: ensuring that interrupted transfers can resume reliably. Many CLIs implement a restart-safe mechanism. This typically involves saving the state of the upload locally, often including the offset within the file, and using a signed URL to resume the upload at that point. The assumption is that when the CLI restarts, it can fetch the signed URL, present it to the upload service, and continue from where it left off.
However, a critical failure mode emerges when the signed resume URL itself expires. Consider a scenario where a large file upload is in progress. The CLI process crashes at a specific point, say 63% completion. The user attempts to restart the CLI to resume the upload. If the signed URL, which was generated with a limited lifespan, has expired by the time the CLI restarts, the resume operation fails. The CLI has the local state (the offset), but it lacks a valid credential to tell the server where to continue. This leaves the user in a state where the upload cannot be resumed, forcing a complete re-upload, which is a poor user experience for large files.
The Solution: Identity-Based Checkpoints, Not Credentials
The core insight to solving this problem lies in decoupling the resume capability from the ephemeral, credential-based signed URL. Instead of relying solely on a temporary URL to resume, the CLI should maintain a persistent checkpoint that identifies the file and the upload session independently of the URL's validity. This checkpoint acts as a stable anchor, allowing the CLI to initiate a new, valid resume URL if the old one has expired.
The structure of such a checkpoint is crucial. It needs to contain enough information for the upload service to recognize the ongoing upload and, importantly, to issue a *new* signed URL for the *current* state. A robust checkpoint typically includes:
- File Identity: A unique identifier for the file being uploaded. This could be a hash of the entire file (e.g., SHA256), or a combination of filename and size if a full hash is too slow to compute upfront.
- Upload Session ID: A server-generated identifier for this specific upload session. This is distinct from the temporary signed URL.
- Local Offset: The byte offset within the local file where the last successful upload chunk completed.
When the CLI restarts, it first checks for a local checkpoint file. If found, it reads the file identity, upload session ID, and local offset. The CLI then contacts the upload service, not with the expired signed URL, but with the upload session ID. The service uses this ID to identify the existing upload session and, if the session is still valid, it generates a *new* signed URL for resuming the upload from the specified local offset. This new URL is then used by the CLI to send the next chunk of data.
This approach is analogous to having a bookmarked page in a book, rather than a ticket to a specific reading session. The bookmark (checkpoint) always tells you where you left off. If the reading session (signed URL) expires, you can simply request a new session with your bookmark, and continue reading from that exact page.

Implementing the Identity Checkpoint
The implementation requires modifications on both the client-side (the CLI) and the server-side (the upload service). On the client, the CLI needs to:
- Generate Checkpoint Data: Before or after each successful chunk upload, the CLI must write a checkpoint file. This file should contain the file's unique identifier (e.g., a computed SHA256 hash), the server-assigned upload ID, and the current local offset.
- Persist Checkpoint: This checkpoint file needs to be stored reliably, perhaps in a dedicated CLI configuration directory.
- Handle Restart: Upon restart, the CLI reads the checkpoint. If no checkpoint exists, it initiates a new upload. If a checkpoint exists, it uses the `uploadId` to request a new resume URL from the server.
- Resume Upload: Using the newly acquired signed URL, the CLI sends the next chunk of data starting from the `localOffset` specified in the checkpoint.
- Update Checkpoint: After each successful chunk upload, the CLI updates the `localOffset` in the checkpoint file. If the entire file is uploaded, the checkpoint file can be deleted.
On the server-side, the upload service must:
- Accept Upload Session ID: The API endpoint for resuming uploads should accept an `uploadId` in addition to, or instead of, a pre-signed URL for initiating the resume.
- Issue New Signed URLs: When an `uploadId` is provided for a resume operation, the server should validate the session and issue a fresh signed URL for the client to use, pointing to the correct offset.
- Manage Upload State: The server needs to maintain the state of upload sessions, including the expected offset for each `uploadId`, to correctly handle subsequent chunk uploads.
The 'Why Now' and Broader Implications
As file sizes continue to grow and network reliability remains a variable, robust upload mechanisms are no longer a luxury but a necessity for any application dealing with large data transfers. Developers building CLIs for cloud storage, software distribution, or data backup services will find this pattern invaluable. It directly addresses a common user pain point: losing progress on large uploads due to unexpected failures or network timeouts, compounded by the transient nature of signed URLs.
The key takeaway is that resume functionality should be anchored by persistent identity, not fleeting credentials. By separating the identifier of an upload session from the temporary access token required to interact with it, CLIs can achieve a level of resilience that was previously difficult to attain. This pattern not only improves the user experience by preventing data loss but also reduces server load by minimizing the need for full re-uploads.
What nobody has addressed yet is the potential for abuse if the `uploadId` itself becomes predictable or easily guessable. Robust security measures, such as generating cryptographically random `uploadId`s and implementing rate limiting on resume requests, will be crucial for production deployments.
