boto3 icon indicating copy to clipboard operation
boto3 copied to clipboard

No Error when creating s3 bucket that already exists using boto3.client and resource.

Open dhineshbabuelango opened this issue 1 year ago • 3 comments

Describe the bug

I created a bucket and then tried to create the another bucket with the same name, through the code hasn't created a new bucket, its not throwing any error but If I use a generic bucket name I am getting the error.

Expected Behavior

I want the code to throw BucketAlreadyExists error.

Current Behavior

In the below screenshot, I tried to create three buckets with the same but no error but when I tried a generic name, it throwed me an error. though the code doesn't create bucket 3 times, why has it not throwed the exception like when I tried to create a bucket named testing image

Reproduction Steps

Reproduce using the below code

image

Python Version: Python 3.12.0

Possible Solution

No response

Additional Information/Context

No response

SDK version used

Python 3.12.0

Environment details (OS name and version, etc.)

Pycharm in Windows

dhineshbabuelango avatar Feb 20 '24 10:02 dhineshbabuelango

Hi @dhineshbabuelango, thanks for reaching out. I was able to reproduce this behavior. The exception that should be thrown here is S3.Client.exceptions.BucketAlreadyOwnedByYou. The resources interface for creating a bucket appears to be lacking this exception. However, because resources are feature-frozen, this is unlikely to change.

I would recommend using the client interface for your use case. The client interface for CreateBucket has exceptions for both your own bucket names and names conflicting with the universal namespace.

Hope that helps, please let me know if you have any other questions.

RyanFitzSimmonsAK avatar Feb 21 '24 20:02 RyanFitzSimmonsAK

Thanks @RyanFitzSimmonsAK, I will try with client

dhineshbabuelango avatar Feb 22 '24 10:02 dhineshbabuelango

Hi @RyanFitzSimmonsAK , Even with client, I am seeing the same issue, its not throwing any error but still creating the bucket.

image

dhineshbabuelango avatar Feb 22 '24 10:02 dhineshbabuelango

This is an old and documented issue with S3 buckets. From the CreateBucket documentation,

BucketAlreadyOwnedByYou The bucket you tried to create already exists, and you own it. Amazon S3 returns this error in all AWS Regions except in the North Virginia Region. For legacy compatibility, if you re-create an existing bucket that you already own in the North Virginia Region, Amazon S3 returns 200 OK and resets the bucket access control lists (ACLs).

If any of those conditions (client region, existing bucket region, LocationConstraint) change from what is described there, it should successfully throw the exception.

RyanFitzSimmonsAK avatar Feb 28 '24 21:02 RyanFitzSimmonsAK

Greetings! It looks like this issue hasn’t been active in longer than five days. We encourage you to check if this is still an issue in the latest release. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or upvote with a reaction on the initial post to prevent automatic closure. If the issue is already closed, please feel free to open a new one.

github-actions[bot] avatar Mar 10 '24 00:03 github-actions[bot]

@RyanFitzSimmonsAK I think this issue should be reopened. @dhineshbabuelango raised a valid issue, and while I understand that this is a behavior of the REST API on which boto3 relies, it should be documented. At this very moment, there is no indication in boto3 documentation that the BucketAlreadyOwnedByYou exception is thrown conditionally. After reading the documentation and looking at the return values and exceptions thrown, I would never have guessed this behavior.

Ideally, the examples section could include a sample demonstrating this non-standard behavior. This would make it much more intuitive for users, as this is not the expected behavior.

mcopik avatar Apr 12 '24 16:04 mcopik