State Persistence and Cross-Session Resumability
What State to Persist
Resumability requires tracking upload state across client restarts. The minimum state: upload ID, file path, total file size, part size, and a list of completed parts with their ETags. The upload ID identifies the session on the server. Completed parts list tells the client what to skip on resume.
Store state to persistent storage before starting each part. If the client crashes mid part, that part is not recorded as complete and will retry. This is idempotent: uploading the same part twice produces the same result. The server overwrites the previous attempt.
State Storage Options
Local file: Write JSON to .upload-state/{uploadId}.json. Simple, works offline. Delete after successful completion. Risk: local storage fills up with orphaned state files from abandoned uploads.
Database: Store in a uploads table. Enables cross device resume: start on laptop, finish on phone. Requires network access to retrieve state. Adds dependency on database availability.
Server side: Some platforms provide list parts API. Client queries server for completed parts instead of tracking locally. Simplest client implementation but requires network round trip before resuming.
Handling File Changes
If the source file changes between sessions, uploaded parts become invalid. Detect this by storing file modification time and hash. On resume, verify the file unchanged. If changed, abort the old upload and start fresh. Uploading parts from different file versions corrupts the final object.
For streaming sources (data generated on the fly), checksums per part enable verification. Store the checksum with part state. On resume, regenerate the part data and verify checksum matches before skipping.
Upload Expiration
Servers expire incomplete uploads after a timeout, typically 7 days. Parts uploaded before expiration are deleted. The upload ID becomes invalid. Clients resuming after expiration must detect this (server returns 404 for the upload ID) and start fresh.
Store upload start time with state. Warn users approaching expiration. For long running uploads (days of intermittent progress), consider completing partial uploads and starting new ones before expiration.