You can try jcabi-s3 (I'm a developer), which does this job for you:

Region region = new Region.Simple("key", "secret");
Bucket bucket = region.bucket("my.example.com");
Ocket.Text ocket = new Ocket.Text(bucket.ocket("test.txt"));
String content = ocket.read();

Check this blog post: http://www.yegor256.com/2014/05/26/amazon-s3-java-oop-adapter.html

Answer from yegor256 on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 73013130 › how-do-i-convert-string-to-s3objectinputstream
java - How do I convert String to S3ObjectInputStream - Stack Overflow
In one of the APIs, S3Object is converted to String using Apache IOUtils using the below steps: S3ObjectInputStream inputStream = s3Object.getObjectContent(); String streamString = IOUtils.toString(
🌐
GitHub
github.com › aws › aws-sdk-java-v2 › issues › 306
Simpler / Easier mechanism to read S3 Object content as a String (like v1 getObjectAsString) · Issue #306 · aws/aws-sdk-java-v2
November 30, 2017 - String result; try (ResponseInputStream<GetObjectResponse> response = client.getObject(request)) { result = IOUtils.toString(response); } catch (IOException e) { throw new UncheckedIOException(e); }
Author   plombardi89
Discussions

amazon web services - How to write an S3 object to a String in java - Stack Overflow
S3Object s3Object = amazonS3Client.getObject(bucketName, key); S3ObjectInputStream stream = s3Object.getObjectContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream)); String text = ""; String temp = ""; try { while((temp = bufferedReader.readLine()) != null){ ... More on stackoverflow.com
🌐 stackoverflow.com
November 25, 2017
string - How to get the value from the s3 input stream in java - Stack Overflow
You could wrap the S3ObjectInputStream within an InputStreamReader and the InputStreamReader within a BufferedInputStream. More on stackoverflow.com
🌐 stackoverflow.com
amazon s3 - How to download s3 object directly into memory in java - Stack Overflow
Is it possible to download S3object in Java directly into memory and get it removed when i'm done with the task? More on stackoverflow.com
🌐 stackoverflow.com
Get an S3Object from a GetObjectResponse in AWS Java SDK 2.0 - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codeflex
codeflex.co › java-read-amazon-s3-object-as-string
Java Read Amazon S3 Object as String | CodeFlex
AWSCredentials credentials = new BasicAWSCredentials("<AWS accesskey>", "<AWS secretkey>"); AmazonS3 s3client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withRegion(Regions.US_EAST_2) .build(); ... public String getS3ObjectContentAsString(String bucketName, String key) { try { if (key.startsWith("/")) { key = key.substring(1); } if (key.endsWith("/")) { key = key.substring(0, key.length()); } try (InputStream is = s3Client.getObject(bucketName, key).getObjectContent()) { return StreamUtils.copyToString(is, StandardCharsets.UTF_8); } } catch (Exception e) { throw new IllegalStateException(e); } } This site uses Akismet to reduce spam.
🌐
AWS
docs.aws.amazon.com › AWSJavaSDK › latest › javadoc › com › amazonaws › services › s3 › model › S3ObjectInputStream.html
S3ObjectInputStream (AWS SDK for Java - 1.12.797)
Input stream representing the content of an S3Object. In addition to the methods supplied by the InputStream class, S3ObjectInputStream supplies the abort() method, which will terminate an HTTP connection to the S3 object.
🌐
Tabnine
tabnine.com › home page › code › java › com.amazonaws.services.s3.model.s3object
com.amazonaws.services.s3.model.S3Object.getObjectContent java code examples | Tabnine
@Override protected InputStream openObjectStream(URI object) throws IOException { try { // Get data of the given object and open an input stream final String bucket = object.getAuthority(); final String key = S3Utils.extractS3Key(object); final S3Object s3Object = s3Client.getObject(bucket, key); if (s3Object == null) { throw new ISE("Failed to get an s3 object for bucket[%s] and key[%s]", bucket, key); } return s3Object.getObjectContent(); } catch (AmazonS3Exception e) { throw new IOException(e); } }
🌐
Program Creek
programcreek.com › java-api-examples
com.amazonaws.services.s3.model.S3ObjectInputStream Java Exaples
/** * convert the S3 Object to String */ public static String convertS3Obj2Str(S3Object s3Obj) throws IOException { S3ObjectInputStream s3is = s3Obj.getObjectContent(); ByteArrayOutputStream fos = new ByteArrayOutputStream(); byte[] read_buf = new byte[1024]; int read_len = 0; try { while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } return fos.toString(ConstantsUnicode.UTF8); } finally { s3is.close(); fos.close(); } }
Find elsewhere
🌐
AWS
docs.aws.amazon.com › AWSJavaSDK › latest › javadoc › com › amazonaws › services › s3 › model › S3Object.html
S3Object (AWS SDK for Java - 1.12.797)
July 25, 2013 - Further, failure to close this ... pool to become blocked. ... An input stream containing the contents of this object. ... Sets the input stream containing this object's contents. ... Sets the input stream containing this object's contents. ... objectContent - The input stream containing this object's contents. Will get wrapped in an S3ObjectInputStream...
🌐
Java2s
java2s.com › example › java-api › com › amazonaws › services › s3 › model › s3objectinputstream › close-0-0.html
Example usage for com.amazonaws.services.s3.model S3ObjectInputStream close
private String readLogFromS3(String stagingBucketName, String key) { Scanner logScanner = null;//from ww w .ja v a 2 s . com S3ObjectInputStream s3ObjectInputStream = null; GZIPInputStream gzipInputStream = null; String lineSeparator = System.getProperty("line.separator"); StringBuilder logContents = new StringBuilder(); S3Object outObject; try { if (s3Client.doesObjectExist(stagingBucketName, key)) { outObject = s3Client.getObject(stagingBucketName, key); s3ObjectInputStream = outObject.getObjectContent(); gzipInputStream = new GZIPInputStream(s3ObjectInputStream); logScanner = new Scanner(gz
🌐
Tabnine
tabnine.com › home page › code › java › com.amazonaws.services.s3.model.s3objectinputstream
com.amazonaws.services.s3.model.S3ObjectInputStream java code examples | Tabnine
S3Object getFile(String key, Path dir) throws FileNotFoundException { S3Object obj = mock(S3Object.class); File file = new File(dir.toString(), key); when(obj.getKey()).thenReturn(file.getName()); S3ObjectInputStream stream = new S3ObjectInputStream(new FileInputStream(file), null); when(obj.getObjectContent()).thenReturn(stream); return obj; }
🌐
AWS
docs.aws.amazon.com › aws sdk for java › developer guide for version 1.x › aws sdk for java code examples › amazon s3 examples using the aws sdk for java › performing operations on amazon s3 objects
Performing Operations on Amazon S3 Objects - AWS SDK for Java 1.x
System.out.format("Downloading %s from S3 bucket %s...\n", key_name, bucket_name); final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build(); try { S3Object o = s3.getObject(bucket_name, key_name); S3ObjectInputStream s3is = o.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File(key_name)); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } catch (FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); }
🌐
Java Tips
javatips.net › api › com.amazonaws.services.s3.model.s3objectinputstream
Java Examples for com.amazonaws.services.s3.model.S3ObjectInputStream
@Override public S3Object ... } // the HTTP request attribute is irrelevant for reading S3ObjectInputStream stream = new S3ObjectInputStream(blobs.get(blobName), null, false); S3Object s3Object = new S3Object(); s3Object.setObjectContent(stream); return s3Object; ...
🌐
Alexwlchan
alexwlchan.net › 2019 › streaming-large-s3-objects
Streaming large objects from S3 with ranged GET requests – alexwlchan
September 12, 2019 - * */ class S3StreamReader(s3Client: AmazonS3, bufferSize: Long) { def get(bucketName: String, key: String): Try[InputStream] = Try { val totalSize = getSize(bucketName, key) val s3Enumeration = getEnumeration(bucketName, key, totalSize) val bufferedEnumeration = getBufferedEnumeration(s3Enumeration) new SequenceInputStream(bufferedEnumeration) } private def getSize(bucketName: String, key: String): Long = s3Client .getObjectMetadata(bucketName, key) .getContentLength private def getEnumeration( bucketName: String, key: String, totalSize: Long): util.Enumeration[S3ObjectInputStream] = new util.
Top answer
1 of 6
41

Since Java 7 (published back in July 2011), there’s a better way: Files.copy() utility from java.util.nio.file.

Copies all bytes from an input stream to a file.

So you need neither an external library nor rolling your own byte array loops. Two examples below, both of which use the input stream from S3Object.getObjectContent().

InputStream in = s3Client.getObject("bucketName", "key").getObjectContent();

1) Write to a new file at specified path:

Files.copy(in, Paths.get("/my/path/file.jpg"));

2) Write to a temp file in system's default tmp location:

File tmp = File.createTempFile("s3test", "");
Files.copy(in, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);

(Without specifying the option to replace existing file, you'll get a FileAlreadyExistsException.)

Also note that getObjectContent() Javadocs urge you to close the input stream:

If you retrieve an S3Object, you should close this input stream as soon as possible, because the object contents aren't buffered in memory and stream directly from Amazon S3. Further, failure to close this stream can cause the request pool to become blocked.

So it should be safest to wrap everything in try-catch-finally, and do in.close(); in the finally block.

The above assumes that you use the official SDK from Amazon (aws-java-sdk-s3).

2 of 6
20

While IOUtils.copy() and IOUtils.copyLarge() are great, I would prefer the old school way of looping through the inputstream until the inputstream returns -1. Why? I used IOUtils.copy() before but there was a specific use case where if I started downloading a large file from S3 and then for some reason if that thread was interrupted, the download would not stop and it would go on and on until the whole file was downloaded.

Of course, this has nothing to do with S3, just the IOUtils library.

So, I prefer this:

InputStream in = s3Object.getObjectContent();
byte[] buf = new byte[1024];
OutputStream out = new FileOutputStream(file);
while( (count = in.read(buf)) != -1)
{
   if( Thread.interrupted() )
   {
       throw new InterruptedException();
   }
   out.write(buf, 0, count);
}
out.close();
in.close();

Note: This also means you don't need additional libraries

🌐
Stack Overflow
stackoverflow.com › questions › 53034665 › java-s3-getobjectcontent
amazon web services - Java S3 getObjectContent - Stack Overflow
If I am trying to save the S3ObjectInputStream I receive the following error: WARNING: Not all bytes were read from the S3ObjectInputStream, aborting HTTP connection. This is likely an error and may result in sub-optimal behavior. Request only the bytes you need via a ranged GET or drain the input stream after use. ... public String handleRequest(S3Event event, Context context) { context.getLogger().log("Received event: " + event.toJson()); // Get the object from the evØent and show its content type String bucket = event.getRecords().get(0).getS3().getBucket().getName(); String key = event.ge