`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.
More on boto3, HTTP & API errors
`botocore.exceptions.NoCredentialsError: Unable to locate credentials`Beginner`botocore.exceptions.NoRegionError: You must specify a region.` even though `AWS_REGION` is set.Intermediate`botocore.exceptions.ClientError: An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.`Beginner`An error occurred (ExpiredToken) when calling the ... operation: The security token included in the request is expired` midway through a long job.Advanced`An error occurred (RequestLimitExceeded) when calling the DescribeInstances operation (reached max retries: 4): Request limit exceeded.`Intermediate`An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid.`Beginner