AWS Security ChangesHomeSearch

AWS AmazonS3 documentation change

Service: AmazonS3 · 2025-08-22 · Documentation low

File: AmazonS3/latest/userguide/access-grants-get-data.md

Summary

Replaced link to external SDK examples with an inline Java code example demonstrating object retrieval, error handling, and resource cleanup

Security assessment

The change adds implementation details for secure resource handling (closing streams) but does not address a specific security vulnerability. While it demonstrates credentials usage and proper cleanup, these are general best practices rather than fixes for documented security issues.

Diff

diff --git a/AmazonS3/latest/userguide/access-grants-get-data.md b/AmazonS3/latest/userguide/access-grants-get-data.md
index 704ee4203..8614bce25 100644
--- a//AmazonS3/latest/userguide/access-grants-get-data.md
+++ b//AmazonS3/latest/userguide/access-grants-get-data.md
@@ -44 +44,90 @@ Java
-For examples of how to get S3 data by using temporary credentials, see how to [get an object by using the AWS SDKs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3_example_s3_GetObject_section.html) and [Amazon S3 code examples for the AWS SDK for Java 2.x](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3#readme).
+The following Java code example gets an object from an S3 bucket. For instructions on creating and testing a working sample, see [Getting Started](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/getting-started.html) in the _AWS SDK for Java Developer Guide_.
+    
+    
+    import com.amazonaws.AmazonServiceException;
+    import com.amazonaws.SdkClientException;
+    import com.amazonaws.auth.profile.ProfileCredentialsProvider;
+    import com.amazonaws.regions.Regions;
+    import com.amazonaws.services.s3.AmazonS3;
+    import com.amazonaws.services.s3.AmazonS3ClientBuilder;
+    import com.amazonaws.services.s3.model.GetObjectRequest;
+    import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
+    import com.amazonaws.services.s3.model.S3Object;
+    
+    import java.io.BufferedReader;
+    import java.io.IOException;
+    import java.io.InputStream;
+    import java.io.InputStreamReader;
+    
+    public class GetObject2 {
+    
+        public static void main(String[] args) throws IOException {
+            Regions clientRegion = Regions.DEFAULT_REGION;
+            String bucketName = "*** Bucket name ***";
+            String key = "*** Object key ***";
+    
+            S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
+            try {
+                AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
+                        .withRegion(clientRegion)
+                        .withCredentials(new ProfileCredentialsProvider())
+                        .build();
+    
+                // Get an object and print its contents.
+                System.out.println("Downloading an object");
+                fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
+                System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
+                System.out.println("Content: ");
+                displayTextInputStream(fullObject.getObjectContent());
+    
+                // Get a range of bytes from an object and print the bytes.
+                GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key)
+                        .withRange(0, 9);
+                objectPortion = s3Client.getObject(rangeObjectRequest);
+                System.out.println("Printing bytes retrieved.");
+                displayTextInputStream(objectPortion.getObjectContent());
+    
+                // Get an entire object, overriding the specified response headers, and print
+                // the object's content.
+                ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
+                        .withCacheControl("No-cache")
+                        .withContentDisposition("attachment; filename=example.txt");
+                GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName, key)
+                        .withResponseHeaders(headerOverrides);
+                headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
+                displayTextInputStream(headerOverrideObject.getObjectContent());
+            } catch (AmazonServiceException e) {
+                // The call was transmitted successfully, but Amazon S3 couldn't process
+                // it, so it returned an error response.
+                e.printStackTrace();
+            } catch (SdkClientException e) {
+                // Amazon S3 couldn't be contacted for a response, or the client
+                // couldn't parse the response from Amazon S3.
+                e.printStackTrace();
+            } finally {
+                // To ensure that the network connection doesn't remain open, close any open
+                // input streams.
+                if (fullObject != null) {
+                    fullObject.close();
+                }
+                if (objectPortion != null) {
+                    objectPortion.close();
+                }
+                if (headerOverrideObject != null) {
+                    headerOverrideObject.close();
+                }
+            }
+        }
+    
+        private static void displayTextInputStream(InputStream input) throws IOException {
+            // Read the text input stream one line at a time and display each line.
+            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                System.out.println(line);
+            }
+            System.out.println();
+        }
+    }
+    
+