hopsey
hopsey

Reputation: 1513

Store byte contents in S3

I need to save binary (byte[]) contents (PDF file) to S3.

I don't want to have a hard copy existing in HDD, pdf is being generated in the fly and then it needs to be sent to S3.

Java AWS SDK AmazonS3.putObject() requires File type in method signature, how can I pass my binary contents directly without saving it to hard drive?

Upvotes: 5

Views: 5462

Answers (2)

Sambit
Sambit

Reputation: 8011

You can use the following method for aws file storage.

PutObjectResult         putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata)

Aws also provides the following overloaded method to use based upon the suitability.

PutObjectResult         putObject(PutObjectRequest putObjectRequest)
PutObjectResult         putObject(String bucketName, String key, File file)
PutObjectResult         putObject(String bucketName, String key, String content)

You can convert byte[] to InputStream and you can pass in the above method. Refer below the javadocs for aws:

Amazon S3

PutObjectRequest

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44960

The SDK offers method that take InputStream and ObjectMetadata instead of File. For example there is putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata).

Use one of the provided methods and supply ByteArrayInputStream if you don't want to create files.

Upvotes: 7

Related Questions