InvalidRange
AWS S3 InvalidRange (HTTP 416) occurs when a GetObject request includes a Range header that exceeds the actual byte size of the object. It indicates the requested offset or length is out of bounds.
Last reviewed: March 26, 2026|Editorial standard: source-backed technical guidance
What Does Invalid Range Mean?
InvalidRange is S3's way of saying: "You are asking for data that does not exist at that position." If an object is 100 bytes and you request bytes=100-200, S3 returns this error. It is most common in parallel download managers and video players that attempt to "pre-fetch" chunks of a file before its total length is verified or if the file was overwritten with a smaller version during the process.
Common Causes
- -Off-by-One Errors: Requesting
bytes=0-100for a 100-byte file (should be0-99as ranges are zero-indexed). - -Empty Files: Attempting any range request on a 0-byte object will always return InvalidRange.
- -Race Conditions: A client requests a range based on a previous version of an object, but the object has since been replaced with a smaller file.
- -Hardcoded Logic: Download managers that assume a fixed chunk size without checking the
Content-Lengthof the target object first.
How to Fix Invalid Range
- 1Perform a HEAD Request: Call
HeadObjectfirst to get the currentContent-Lengthand validate your range before callingGetObject. - 2Zero-Index Validation: Ensure your range ends at
Length - 1. For a file of size N, the valid range is0toN-1. - 3Conditional Headers: Use the
If-Matchheader with the object's ETag to ensure you are requesting a range from the exact version of the file you expect. - 4Handle 0-byte Objects: Add a conditional check to skip range requests if the object size is zero.
Step-by-Step Diagnosis for Invalid Range
- 1Check the
Content-Lengthof the object in the S3 console or via CLI to find the actual boundaries. - 2Inspect the
Rangeheader being sent by the client. Format:bytes=start-end. - 3Verify the object has not been updated recently by checking the
Last-Modifiedtimestamp in S3 metadata. - 4Enable S3 Server Access Logs to see the exact Range string that triggered the 416 error.
Range Request Math 101
- -Total Size (N): 1024 bytes.
- -Valid Max Range:
bytes=0-1023. - -Invalid Range:
bytes=0-1024(This requests 1025 bytes, triggering InvalidRange).
Streaming & Parallel Downloads
- -Video players often request the last 10KB of a file to read metadata (MOOV atom). If the file is smaller than 10KB, this "suffix" request (
bytes=-10240) will fail with InvalidRange.
Implementation Examples
# 1. Get the actual size
SIZE=$(aws s3api head-object --bucket my-bucket --key file.mp4 --query 'ContentLength')
# 2. Calculate safe end (Size - 1)
SAFE_END=$((SIZE - 1))
# 3. Perform safe range request
aws s3api get-object --bucket my-bucket --key file.mp4 --range bytes=0-$SAFE_END output.partconst { S3Client, HeadObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
async function downloadSafeRange(bucket, key, start) {
const head = await client.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
const totalSize = head.ContentLength;
if (start >= totalSize) {
console.error("Start byte is beyond file size!");
return;
}
// Blank end 'start-' is the safest way to avoid InvalidRange
return client.send(new GetObjectCommand({
Bucket: bucket,
Key: key,
Range: `bytes=${start}-`
}));
}How to Verify the Fix
- -Trigger a
GetObjectwith a range that is strictly within0toContent-Length - 1. - -Confirm the response is
206 Partial Contentand not416 Requested Range Not Satisfiable. - -Verify the returned
Content-Rangeheader matches your requested boundaries.
How to Prevent Recurrence
- -Dynamic Range Calculation: Never hardcode byte offsets. Always derive them from the
Content-Lengthreturned by aHeadObjectcall. - -ETag Locking: When performing multipart downloads, "lock" the version of the file using the
If-Matchheader to prevent race conditions during updates. - -Sanitize Inputs: In frontend code, ensure that start/end values are integers and that
start < end. - -Pro-tip: If you want to download from a specific point to the end of the file, use
bytes=start-(leave the end blank). S3 will automatically return everything from the start point to the actual end of the file, safely avoiding InvalidRange errors.
Decision Support
Official References
Provider Context
This guidance is specific to AWS services. Always validate implementation details against official provider documentation before deploying to production.