AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2026-06-16 · Documentation low

File: code-library/latest/ug/java_2_s3_code_examples.md

Summary

Added Java code example for checking S3 object existence using HeadObject

Security assessment

The change adds a standard object existence check method with notes about 403/404 behavior, but doesn't address any specific vulnerability or security feature.

Diff

diff --git a/code-library/latest/ug/java_2_s3_code_examples.md b/code-library/latest/ug/java_2_s3_code_examples.md
index 96a3ccf93..7e9e344f7 100644
--- a//code-library/latest/ug/java_2_s3_code_examples.md
+++ b//code-library/latest/ug/java_2_s3_code_examples.md
@@ -4675,0 +4676,74 @@ You can use the following `doesBucketExists` method as a replacement for the the
+The following code example shows how to check if an object exists.
+
+**SDK for Java 2.x**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3#code-examples). 
+
+You can use the following `doesObjectExist` method as a replacement for the SDK for Java V1 [AmazonS3Client#doesObjectExist(String, String)](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#doesObjectExist-java.lang.String-java.lang.String-) method.
+    
+    
+    import org.slf4j.Logger;
+    import org.slf4j.LoggerFactory;
+    import software.amazon.awssdk.services.s3.S3Client;
+    import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
+    import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
+    import software.amazon.awssdk.utils.Validate;
+    
+    public class DoesObjectExist {
+        private static final Logger logger = LoggerFactory.getLogger(DoesObjectExist.class);
+    
+        public static void main(String[] args) {
+            DoesObjectExist doesObjectExist = new DoesObjectExist();
+    
+            final S3Client s3SyncClient = S3Client.builder().build();
+            final String bucketName = "amzn-s3-demo-bucket"; // Replace with your bucket name.
+            final String key = "my-key"; // Replace with your object key.
+    
+            boolean exists = doesObjectExist.doesObjectExist(bucketName, key, s3SyncClient);
+            logger.info("Object exists: {}", exists);
+        }
+    
+        /**
+         * Checks if the specified object exists in the specified bucket.
+         * <p>
+         * Internally this method uses the
+         * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html#headObject(java.util.function.Consumer)">S3Client.headObject</a>
+         * operation to determine whether the object exists.
+         * <p>
+         * This method is equivalent to the AWS SDK for Java V1's
+         * <a href="https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#doesObjectExist-java.lang.String-java.lang.String-">AmazonS3Client#doesObjectExist(String, String)</a>.
+         * <p>
+         * <b>Note:</b> This method returns {@code false} only when S3 responds with 404 (NoSuchKey). If the caller
+         * does not have {@code s3:ListBucket} permission on the bucket, S3 may return 403 instead of 404 for
+         * non-existent objects, which will be thrown as an exception.
+         *
+         * @param bucketName   The name of the bucket containing the object.
+         * @param key          The key of the object to check.
+         * @param s3SyncClient An {@code S3Client} instance.
+         * @return {@code true} if the object exists; {@code false} if it does not exist.
+         */
+        public boolean doesObjectExist(String bucketName, String key, S3Client s3SyncClient) {
+            try {
+                Validate.notEmpty(bucketName, "The bucket name must not be null or an empty string.", "");
+                Validate.notEmpty(key, "The object key must not be null or an empty string.", "");
+                s3SyncClient.headObject(HeadObjectRequest.builder()
+                        .bucket(bucketName)
+                        .key(key)
+                        .build());
+                return true;
+            } catch (NoSuchKeyException e) {
+                return false;
+            }
+        }
+    }
+    
+    
+
+  * For API details, see [HeadObject](https://docs.aws.amazon.com/goto/SdkForJavaV2/s3-2006-03-01/HeadObject) in _AWS SDK for Java 2.x API Reference_. 
+
+
+
+