question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

s3 head_object returns ClientError 404 instead of NoSuchKey error

See original GitHub issue

What issue did you see ?

When the key does not exist in s3, s3 head_object returns a ClientError 404 instead of NoSuchKey error, making it harder to catch. Also, the doc says it should throw a NoSuchKey error. But get_object returns NoSuchKey.

Steps to reproduce

client.head_object(
    Bucket=bucket,
    Key=key,
)  # throws ClientError with code 404

client.get_object(
    Bucket=bucket,
    Key=key,
)  # throws NoSuchKey

Is this expected? If so, should the docs be modified? Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:20
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
adonigcommented, Sep 10, 2021

As a temporary workaround (implying the bug will get fixed one day) you can catch the ClientError like this:

import botocore
import boto3

s3_client = boto3.client("s3")
try:
    response = s3_client.head_object(Bucket="mybucket", Key="mykey")
except s3_client.exceptions.NoSuchKey:  # type: ignore
    pass   # TODO: Handle the exception here.
except botocore.exceptions.ClientError as err:
    # NOTE: This case is required because of https://github.com/boto/boto3/issues/2442
    if err.response["Error"]["Code"] == "404":
        pass  # TODO: Handle the exception here again.

That way your code will still work as expected in case the bug gets fixed.

0reactions
tim-finnigancommented, Nov 21, 2022

Checking in - I believe the docs have been updated since this original issue was opened. In the head_object and get_object boto3 docs it now says:

  • If you have the s3:ListBucket permission on the bucket, Amazon S3 returns an HTTP status code 404 (“no such key”) error.
  • If you don’t have the s3:ListBucket permission, Amazon S3 returns an HTTP status code 403 (“access denied”) error.

If that still doesn’t clarify the behavior then any further requests for improvements to the documentation should be redirected to the S3 team because they own their API documentation as mentioned here earlier.

There is a Provide feedback link at the bottom of every API page where you can send direct feedback to the appropriate team. Alternatively you can reach out through AWS Support or we can forward requests internally. Please let us know if you had any follow up questions on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

s3 HeadObject returning object metadata but boto3 Copy ...
According to AWS documentation and stack overflow community, the best way to deal with 404 (NoSuchKey) error for read after right is to...
Read more >
Downloading a File from an S3 Bucket - Amazon AWS
If the service returns a 404 error, it prints an error message indicating that the object doesn't exist. import boto3 import botocore BUCKET_NAME...
Read more >
AWS S3 Error executing "HeadObject" / AWS HTTP error
Hello, I want use S3, but i have some problems in localhost I started with an arror where i need ... But you're...
Read more >
Middleware — Swift 2.31.0.dev103 documentation
The s3api middleware will emulate the S3 REST api on top of swift. ... or the symlink creation will return a client error....
Read more >
IBM Cloud Object Storage S3 API
This operation returns a list of all buckets within a service instance. ... with an incorrect location will result in a 404 NoSuchKey...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found