aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

S3#bucketExists and S3#objectExists

Open jhovell opened this issue 7 years ago • 8 comments

Expected Behavior

I would expect feature parity for old 1.x methods such as AmazonS3.doesObjectExist(bucket, key). With the async client / CompletableFuture it's more than a minor annoyance as the result of S3AsyncClient.headObject(...) is to throw a NoSuchKeyException which would logically be handled in an exceptionally(throwable -> ...) stage but it is difficult to handle control flow such as letting other exceptions propagate.

Your Environment

  • AWS Java SDK version used: 2.0.0-preview-7
  • JDK version used: Oracle Java(TM) SE Runtime Environment (build 9+181) Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
  • Operating System and version: OSX

jhovell avatar Jan 17 '18 07:01 jhovell

+1, there needs to be more high-level operations on the S3 client. Eg. doesBucketExist, doesObjectExist, etc.

Right now, the raw S3 operations require using exceptions for non-exceptional cases.

millems avatar Jan 17 '18 19:01 millems

most of the time checking for file/bucket existence buys you nothing, by the time you act on it it might already change it's state (be deleted, be created) so unwanted state exception need to be correctly handled anyway , so whats the point of checking then :)

redbaron avatar Jan 24 '19 13:01 redbaron

most of the time checking for file/bucket existence buys you nothing, by the time you act on it it might already change it's state (be deleted, be created)

There are plenty of systems that simply never delete s3 objects, so once you check if it exists, that won't ever change.

Also in Java I believe it's generally frowned upon to throw/catch Exceptions for situations which are expected to happen.

I think it would be useful to have a doesObjectExist method.

aiba avatar Oct 28 '19 23:10 aiba

This issue is still open, so I guess this feature isn't added as of now. Correct?

I was already using the doesObjectExists in the 'aws-java-sdk-s3'. I was going to the non-blocking method but since this method doesn't exist in the java 2.x, it's a spoiler. :/

anmgarg avatar Mar 01 '21 08:03 anmgarg

To be clear: you can use the headObject method, but it's just a bit messy because you need to catch the exception if it doesn't exist.

millems avatar Jul 14 '21 21:07 millems

Throwing and catching exceptions hundreds of thousands of times has a performance cost that would be good to avoid, any progress on this issue?

laymain avatar Jan 13 '22 17:01 laymain

the exception is not thrown any way. is there a way to know if a bucket exist??

yehudasag avatar Jan 26 '22 20:01 yehudasag

Agreed with everyone above who says we need this in the Java SDK.

JoshMcCullough avatar Jun 21 '22 02:06 JoshMcCullough

Yes, I also think that controlling the flow with the exception doesn't make any sense as it's an expensive operation and rather should be an "exception". +1 for having this method.

fatih-celonis avatar Jan 05 '23 15:01 fatih-celonis