Resumable Upload with Range Reconciliation
For large uploads, stream data in 256KB-aligned chunks and trust YouTube's Range header as the source of truth.
Required Behavior
- Chunk size: multiples of
256KB(except final chunk). - Send
Content-Range: bytes start-end/totalfor each PUT. - Handle responses:
201: upload complete, parse video resource ID.308: incomplete, parseRange: bytes=0-XXXXand continue fromXXXX + 1.
Critical Rule
Never assume a full chunk was accepted. Use returned Range when present.
const range = response.headers.get('Range');
if (range) {
const match = range.match(/bytes=0-(\d+)/);
bytesUploaded = match ? Number(match[1]) + 1 : bytesUploaded + chunk.length;
}
Streaming Pattern
- Read from
ReadableStream. - Buffer until threshold (or final chunk).
- Upload chunk.
- Reset buffer and continue.
Thumbnail Follow-up
After main upload, upload thumbnail separately:
- hard cap 2MB
- fallback resize/quality ladder (e.g. 1280x720@95 -> smaller/lower quality)
- treat thumbnail failure as non-fatal if main video succeeded