I am getting an AWS S3 image resource as a S3ObjectInputStream and want to put it into a Vaadin image HTML Component. The Vaadin Image only has two ways of initialization. The first is through a string indicating a filepath, which would mean writing the S3 image to disk, which I cannot do because of data security reasons and the second one is through something implementing "AbstractStreamResource". The question now is, how do I get the S3ObjectInputStream converted to something implementing "AbstractStreamResource" to keep the file only in memory.
CodePudding user response:
Based on S3ObjectInputStream API doc S3ObjectInputStream is sub class of java.io.InputStream. In that case you should be able to do
S3ObjectInputStream s3ImageResource = ...
StreamResource res = new StreamResource("filename.jpg", () -> {
InputStream is = s3ImageResource;
return is;
});
Image image = new Image(res,"Image");
Alternatively if that does not work, based on other questions here in StackOverflow, I assume you can convert S3ObjectInputStream to byte array.
byte[] bytes = // get bytes from S3ObjectInputStream
StreamResource res = new StreamResource("filename.jpg", () -> {
InputStream is = new ByteArrayInputStream(bytes);
return is;
});
Image image = new Image(res,"Image");