AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-04-11 · Documentation low

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

Summary

Updated Rekognition examples to use S3 bucket references instead of local files, added Javadoc comments, and standardized region usage

Security assessment

Changes focus on code structure and S3 integration rather than addressing security vulnerabilities. No security advisories or vulnerability fixes mentioned.

Diff

diff --git a/code-library/latest/ug/java_2_rekognition_code_examples.md b/code-library/latest/ug/java_2_rekognition_code_examples.md
index 679686d4c..ca396831a 100644
--- a//code-library/latest/ug/java_2_rekognition_code_examples.md
+++ b//code-library/latest/ug/java_2_rekognition_code_examples.md
@@ -44,7 +44 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    import software.amazon.awssdk.services.rekognition.model.RekognitionException;
-    import software.amazon.awssdk.services.rekognition.model.Image;
-    import software.amazon.awssdk.services.rekognition.model.CompareFacesRequest;
-    import software.amazon.awssdk.services.rekognition.model.CompareFacesResponse;
-    import software.amazon.awssdk.services.rekognition.model.CompareFacesMatch;
-    import software.amazon.awssdk.services.rekognition.model.ComparedFace;
-    import software.amazon.awssdk.services.rekognition.model.BoundingBox;
+    import software.amazon.awssdk.services.rekognition.model.*;
@@ -69,2 +63 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    
-                Usage:    <pathSource> <pathTarget>
+                Usage: <bucketName> <sourceKey> <targetKey>
@@ -73,2 +66,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                   pathSource - The path to the source image (for example, C:\\AWS\\pic1.png).\s
-                    pathTarget - The path to the target image (for example, C:\\AWS\\pic2.png).\s
+                    bucketName - The name of the S3 bucket where the images are stored.
+                    sourceKey  - The S3 key (file name) for the source image.
+                    targetKey  - The S3 key (file name) for the target image.
@@ -77 +71 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            if (args.length != 2) {
+            if (args.length != 3) {
@@ -82,4 +76,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            Float similarityThreshold = 70F;
-            String sourceImage = args[0];
-            String targetImage = args[1];
-            Region region = Region.US_EAST_1;
+            String bucketName = args[0];
+            String sourceKey = args[1];
+            String targetKey = args[2];
+    
+            Region region = Region.US_WEST_2;
@@ -89,3 +84 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    
-            compareTwoFaces(rekClient, similarityThreshold, sourceImage, targetImage);
-            rekClient.close();
+            compareTwoFaces(rekClient, bucketName, sourceKey, targetKey);
@@ -94,2 +87,13 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        public static void compareTwoFaces(RekognitionClient rekClient, Float similarityThreshold, String sourceImage,
-                                           String targetImage) {
+        /**
+         * Compares two faces from images stored in an Amazon S3 bucket using AWS Rekognition.
+         *
+         * <p>This method takes two image keys from an S3 bucket and compares the faces within them.
+         * It prints out the confidence level of matched faces and reports the number of unmatched faces.</p>
+         *
+         * @param rekClient   The {@link RekognitionClient} used to call AWS Rekognition.
+         * @param bucketName  The name of the S3 bucket containing the images.
+         * @param sourceKey   The object key (file path) for the source image in the S3 bucket.
+         * @param targetKey   The object key (file path) for the target image in the S3 bucket.
+         * @throws RuntimeException If the Rekognition service returns an error.
+         */
+        public static void compareTwoFaces(RekognitionClient rekClient, String bucketName, String sourceKey, String targetKey) {
@@ -97,4 +101,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                InputStream sourceStream = new FileInputStream(sourceImage);
-                InputStream tarStream = new FileInputStream(targetImage);
-                SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
-                SdkBytes targetBytes = SdkBytes.fromInputStream(tarStream);
+                Float similarityThreshold = 70F;
+                S3Object s3ObjectSource = S3Object.builder()
+                        .bucket(bucketName)
+                        .name(sourceKey)
+                        .build();
@@ -102,3 +107,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                // Create an Image object for the source image.
-                Image souImage = Image.builder()
-                    .bytes(sourceBytes)
+                Image sourceImage = Image.builder()
+                        .s3Object(s3ObjectSource)
+                        .build();
+    
+                S3Object s3ObjectTarget = S3Object.builder()
+                        .bucket(bucketName)
+                        .name(targetKey)
@@ -107,2 +116,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                Image tarImage = Image.builder()
-                    .bytes(targetBytes)
+                Image targetImage = Image.builder()
+                        .s3Object(s3ObjectTarget)
@@ -112,2 +121,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    .sourceImage(souImage)
-                    .targetImage(tarImage)
+                        .sourceImage(sourceImage)
+                        .targetImage(targetImage)
@@ -129,4 +137,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                List<ComparedFace> uncompared = compareFacesResult.unmatchedFaces();
-                System.out.println("There was " + uncompared.size() + " face(s) that did not match");
-                System.out.println("Source image rotation: " + compareFacesResult.sourceImageOrientationCorrection());
-                System.out.println("target image rotation: " + compareFacesResult.targetImageOrientationCorrection());
@@ -134,2 +139,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            } catch (RekognitionException | FileNotFoundException e) {
-                e.printStackTrace();
+                List<ComparedFace> unmatchedFaces = compareFacesResult.unmatchedFaces();
+                System.out.println("There were " + unmatchedFaces.size() + " face(s) that did not match.");
+    
+            } catch (RekognitionException e) {
+                System.err.println("Error comparing faces: " + e.awsErrorDetails().errorMessage());
+                throw new RuntimeException(e);
@@ -189 +198 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            Region region = Region.US_EAST_1;
+            Region region = Region.US_WEST_2;
@@ -198,0 +208,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        /**
+         * Creates a new Amazon Rekognition collection.
+         *
+         * @param rekClient    the Amazon Rekognition client used to interact with the Rekognition service
+         * @param collectionId the unique identifier for the collection to be created
+         */
@@ -274,0 +289,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        /**
+         * Deletes an Amazon Rekognition collection.
+         *
+         * @param rekClient      An instance of the {@link RekognitionClient} class, which is used to interact with the Amazon Rekognition service.
+         * @param collectionId   The ID of the collection to be deleted.
+         */
@@ -335 +353 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            if (args.length != 1) {
+            if (args.length != 2) {
@@ -351,0 +370,8 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        /**
+         * Deletes a face from the specified Amazon Rekognition collection.
+         *
+         * @param rekClient     an instance of the Amazon Rekognition client
+         * @param collectionId  the ID of the collection from which the face should be deleted
+         * @param faceId        the ID of the face to be deleted
+         * @throws RekognitionException if an error occurs while deleting the face
+         */
@@ -429,0 +455,8 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        /**
+         * Describes an Amazon Rekognition collection.
+         *
+         * @param rekClient         The Amazon Rekognition client used to make the request.
+         * @param collectionName    The name of the collection to describe.
+         *
+         * @throws RekognitionException If an error occurs while describing the collection.
+         */
@@ -469,11 +502,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    import software.amazon.awssdk.services.rekognition.model.RekognitionException;
-    import software.amazon.awssdk.services.rekognition.model.DetectFacesRequest;
-    import software.amazon.awssdk.services.rekognition.model.DetectFacesResponse;
-    import software.amazon.awssdk.services.rekognition.model.Image;
-    import software.amazon.awssdk.services.rekognition.model.Attribute;
-    import software.amazon.awssdk.services.rekognition.model.FaceDetail;
-    import software.amazon.awssdk.services.rekognition.model.AgeRange;
-    import software.amazon.awssdk.core.SdkBytes;
-    import java.io.FileInputStream;
-    import java.io.FileNotFoundException;
-    import java.io.InputStream;
+    import software.amazon.awssdk.services.rekognition.model.*;
+    
@@ -485 +509 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-     *
+     * <p>
@@ -487 +511 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-     *
+     * <p>
@@ -494 +518 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    Usage:    <sourceImage>
+                Usage:   <bucketName> <sourceImage>
@@ -497 +521,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                       sourceImage - The path to the image (for example, C:\\AWS\\pic1.png).\s
+                    bucketName = The name of the Amazon S3 bucket where the source image is stored.
+                    sourceImage - The name of the source image file in the Amazon S3 bucket. (for example, pic1.png).\s
@@ -500 +525 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            if (args.length != 1) {
+            if (args.length != 2) {
@@ -505,2 +530,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            String sourceImage = args[0];
-            Region region = Region.US_EAST_1;
+            String bucketName = args[0];
+            String sourceImage = args[1];
+            Region region = Region.US_WEST_2;
@@ -511 +537 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            detectFacesinImage(rekClient, sourceImage);
+            detectFacesinImage(rekClient, bucketName, sourceImage);
@@ -515 +541,8 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        public static void detectFacesinImage(RekognitionClient rekClient, String sourceImage) {
+        /**
+         * Detects faces in an image stored in an Amazon S3 bucket using the Amazon Rekognition service.
+         *
+         * @param rekClient    The Amazon Rekognition client used to interact with the Rekognition service.
+         * @param bucketName   The name of the Amazon S3 bucket where the source image is stored.
+         * @param sourceImage  The name of the source image file in the Amazon S3 bucket.
+         */
+        public static void detectFacesinImage(RekognitionClient rekClient, String bucketName, String sourceImage) {
@@ -517,2 +550,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                InputStream sourceStream = new FileInputStream(sourceImage);
-                SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
+                S3Object s3ObjectTarget = S3Object.builder()
+                    .bucket(bucketName)
+                    .name(sourceImage)
+                    .build();
@@ -520,3 +555,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                // Create an Image object for the source image.
-                Image souImage = Image.builder()
-                        .bytes(sourceBytes)