Manisha
Manisha

Reputation: 815

How to download s3 object directly into memory in java

Is it possible to download S3object in Java directly into memory and get it removed when i'm done with the task?

Upvotes: 32

Views: 44155

Answers (4)

Cemalettin Altınsoy
Cemalettin Altınsoy

Reputation: 137

This is the best practise.

S3Object object = s3.getObject("bucket", "key"); 
byte[] byteArray = IOUtils.toByteArray(object.getObjectContent());

Upvotes: 1

togise
togise

Reputation: 298

As of AWS JAVA SDK 2 you can you use ReponseTransformer to convert the response to different types. (See javadoc).

Below is the example for getting the object as bytes

GetObjectRequest request = GetObjectRequest.builder().bucket(bucket).key(key).build()
ResponseBytes<GetObjectResponse> result = bytess3Client.getObject(request, ResponseTransformer.toBytes())

// to get the bytes
result.asByteArray()

Upvotes: 8

benderalex5
benderalex5

Reputation: 137

For example, convert file data to string:

S3Object s3object = s3.getObject(new GetObjectRequest(bucketName, key));
S3ObjectInputStream inputStream = s3object.getObjectContent();
StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

Upvotes: 1

Zach Musgrave
Zach Musgrave

Reputation: 1022

Use the AWS SDK for Java and Apache Commons IO as such:

//import org.apache.commons.io.IOUtils

AmazonS3 s3  = new AmazonS3Client(credentials);  // anonymous credentials are possible if this isn't your bucket
S3Object object = s3.getObject("bucket", "key"); 
byte[] byteArray = IOUtils.toByteArray(object.getObjectContent());

Not sure what you mean by "get it removed", but IOUtils will close the object's input stream when it's done converting it to a byte array. If you mean you want to delete the object from s3, that's as easy as:

s3.deleteObject("bucket", "key"); 

Upvotes: 58

Related Questions