Interview Prep/Python for DevOps/boto3, HTTP & API errors

`botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation`, but `head_object` on another key returns `ClientError` with code `404`. Why the inconsistency?

AdvancedTroubleshootingboto3, HTTP & API errors
Answer

Likely causes → GetObject returns an XML error body with code NoSuchKey, which boto3 maps to the modelled exception. HeadObject responses have no body, so the error code is just the HTTP status 404. Also, without s3:ListBucket permission, S3 returns 403 AccessDenied for missing keys instead of 404.

Diagnose →

except ClientError as e: print(e.response["Error"]["Code"], e.response["ResponseMetadata"]["HTTPStatusCode"])

Fix → Handle both: if e.response["Error"]["Code"] in ("404", "NoSuchKey"). Grant s3:ListBucket if you need to distinguish missing from forbidden.

Prevent → Wrap S3 existence checks in one helper with tests for each case.

All Python for DevOps questions Roadmap: Python for automation

More on boto3, HTTP & API errors