Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boto3.client("s3").head_bucket does not throw NoSuchBucket as per documentation #2499

Closed
ashaik687 opened this issue Jul 6, 2020 · 9 comments
Assignees
Labels
closed-for-staleness service-api This issue is caused by the service API, not the SDK implementation. wontfix We have determined that we will not resolve the issue.

Comments

@ashaik687
Copy link

Contrary to documentation, boto3.client("s3").head_bucket(Bucket=bucket_name) does not throw S3.Client.exceptions.NoSuchBucket. It throws An error occurred (404) when calling the HeadBucket operation: Not Found
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.head_bucket

import boto3
client = boto3.client("s3")
try:
    client.head_bucket(Bucket='bucket-does-not-exist')
except client.exceptions.NoSuchBucket:
    print('exception caught')

The above code snippet should print 'exception caught'. However, It throws the below exception.

  File "/Users/ashaik/.local/share/virtualenvs/loadenv38-el3BARCC/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/ashaik/.local/share/virtualenvs/loadenv38-el3BARCC/lib/python3.8/site-packages/botocore/client.py", line 635, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (404) when calling the HeadBucket operation: Not Found

Please align documentation and behavior of exception thrown.

@ashaik687 ashaik687 added the needs-triage This issue or PR still needs to be triaged. label Jul 6, 2020
@swetashre swetashre self-assigned this Jul 7, 2020
@swetashre
Copy link
Contributor

@ashaik687 - Thank you for your post. The API model provided by S3 that the AWS SDKs use to generate the code and documentation for their S3 clients shows NoSuchBucket as the error shape for a HeadBucket operation.
Since HEAD operations don't return an HTTP response body the SDK can't convert the response to a modeled error so instead it just returns a generic HTTP 404 response. So it need to be fixed on s3 side.
According to the s3 team service will not be changing its behavior regarding the error returned on HeadBucket requests to nonexistent resources.

Unfortunately we won't be able to make the requested change in our documentation to account for this. All of the AWS SDKs use the same model provided to us by the S3 team to generate the code and documentation for their S3 clients, unfortunately the way in which the model is written by the S3 team does not allow for removing the constant used for NoSuchBucket from this operation without creating a breaking change.

@swetashre swetashre added service-api This issue is caused by the service API, not the SDK implementation. closing-soon This issue will automatically close in 4 days unless further comments are made. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 7, 2020
@benkehoe
Copy link

benkehoe commented Jul 8, 2020

There's no way for the S3 model to indicate that a 404 error from HeadBucket should be converted to a NoSuchBucket error?

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jul 8, 2020
@swetashre
Copy link
Contributor

@benkehoe - Since HeadBucket operations doesn't return an HTTP response body the SDK can't convert the response to a modeled error so instead it just returns a generic HTTP 404 response.
In this case s3 will have to update their model with the correct exception as AWS SDKs use those api model to generate documentation.

@swetashre swetashre added closing-soon This issue will automatically close in 4 days unless further comments are made. wontfix We have determined that we will not resolve the issue. labels Jul 8, 2020
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jul 13, 2020
@BastianZim
Copy link
Contributor

@stobrien89 Sorry to be pinging you out of the blue but would it be possible to add a note or something somewhere for this, as I just stumbled over the same thing. Also, can we expect this to never change or might this be something that gets fixed in the future?

@stobrien89
Copy link
Contributor

stobrien89 commented Sep 14, 2021

Hi @BastianZim,

My apologies for the delayed response. It seems like something could be done from a documentation standpoint since it seems they won't be changing the API behavior (I'll double-check on that), so I'll see if they're able to fix this upstream.

@BastianZim
Copy link
Contributor

Thank you @stobrien89!

@stobrien89
Copy link
Contributor

Hi again @BastianZim,

Had a chance to look into this further this morning and it looks like the documentation has been updated:

If the bucket does not exist or you do not have permission to access it, the HEAD request returns a generic 404 Not Found or 403 Forbidden code. A message body is not included, so you cannot determine the exception beyond these error codes.

From what I understand, they won't be able to remove the error from the model, as it would be a breaking change, so it will remain listed under the exceptions section, which is derived from the s3 model. Wish there was more we could do here!

@BastianZim
Copy link
Contributor

Hi @stobrien89 ok thanks for looking into this again though!

@m-skarzyn
Copy link

m-skarzyn commented Jan 24, 2023

Method which works:

import boto3
import botocore

client = boto3.client("s3")
try:
    client.head_bucket(Bucket='bucket-does-not-exist')
except botocore.exceptions.ClientError:
    print('exception caught')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness service-api This issue is caused by the service API, not the SDK implementation. wontfix We have determined that we will not resolve the issue.
Projects
None yet
Development

No branches or pull requests

6 participants