Reputation: 1513
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
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:
Upvotes: 0
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