AWS
Boto3
S3
Python
HeadObject Error

AWS BOTO3 S3 python - An error occurred 404 when calling the HeadObject operation Not Found

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

A 404 from S3 HeadObject means the requested key is not found in the bucket path you provided, or the request context cannot see that object. The error is common in automation pipelines where bucket, key, region, or account assumptions drift. Good diagnostics focus on request identity and exact object path first.

Understand What HeadObject Checks

HeadObject retrieves metadata without downloading object content. It is useful for existence checks and metadata driven logic.

python
1import boto3
2from botocore.exceptions import ClientError
3
4s3 = boto3.client("s3", region_name="us-east-1")
5
6bucket = "my-bucket"
7key = "reports/2026-03-01/output.json"
8
9try:
10    response = s3.head_object(Bucket=bucket, Key=key)
11    print("exists", response["ContentLength"])
12except ClientError as exc:
13    code = exc.response.get("Error", {}).get("Code")
14    print("head_object failed", code)

If code is 404 or NoSuchKey, verify path and context before retry loops.

Fast Debugging Checklist

  1. Confirm bucket name exactly, including account and environment suffixes.
  2. Confirm key case and prefix separators.
  3. Confirm object was uploaded to the same region.
  4. Confirm credentials point to expected AWS account.
  5. Confirm no leading slash in Key value.

Many incidents are simple key construction bugs such as accidental double slashes or wrong date partition.

Validate With Listing And Structured Key Builders

Use a list call on the expected prefix to confirm what exists.

python
resp = s3.list_objects_v2(Bucket=bucket, Prefix="reports/2026-03-01/")
for obj in resp.get("Contents", []):
    print(obj["Key"])

Build keys through one helper function instead of string concatenation across multiple modules.

python

def build_report_key(run_date: str, filename: str) -> str:
    return f"reports/{run_date}/{filename}"

Centralized key construction prevents subtle mismatch bugs.

Permissions And Error Interpretation

Insufficient permissions can sometimes appear similar to not found from an application perspective. Check IAM policies for s3:HeadObject and relevant bucket policy conditions. In cross account setups, validate both role permissions and bucket side policy.

Include request id and host id from exception metadata in logs. These identifiers are useful when escalating to AWS support or internal cloud platform teams.

Reliability Patterns

For eventually consistent workflows between producers and consumers, add bounded retries with jitter for recently written objects. Keep retries short and observable. If object creation is event driven, prefer explicit completion signals over blind polling.

Versioning, Encryption, And Cross Account Edge Cases

If bucket versioning is enabled, verify whether your workflow expects a specific version id. A plain HeadObject checks the latest version by default, which can differ from historical references in event payloads.

For encrypted objects, ensure the calling principal has both object read permissions and required key access when using customer managed keys. Missing key permissions can produce confusing application level behavior around object access checks.

Cross account data lakes add another layer: identity based policy in caller account and resource policy in bucket account must both allow the action. During troubleshooting, print caller account id and role ARN in logs to avoid debugging against the wrong trust relationship.

Common Pitfalls

  • Passing wrong key prefix or wrong case in object key.
  • Using credentials for a different account than expected.
  • Forgetting region alignment for bucket operations.
  • Building keys in many places and introducing inconsistent paths.
  • Treating all 404 outcomes as transient and retrying forever.

Summary

  • HeadObject 404 usually indicates path or context mismatch.
  • Start with bucket, key, region, and account validation.
  • Use prefix listing and centralized key builders for correctness.
  • Verify IAM and bucket policies for metadata access.
  • Add bounded retries only when workflow timing justifies them.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design