Reputation: 327
So my problem's that when I open the URL of the uploaded file, it downloads it. I did some research and the problem's that I should somehow specify to S3 that its an image. Currently this's the function that handles the upload but I don't know how could I set the type there. Or is there a built in function that recognises the images and when the URL is opened it shows them automatically rather than downloads them?
Thanks
public void writeResource(byte[] bytes, String fileName) throws IOException
{
Resource resource = this.resourceLoader.getResource("s3://bucket/" + fileName);
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream())
{
outputStream.write(bytes);
}
}
Upvotes: 4
Views: 3036
Reputation: 327
@Autowired
private AmazonS3 amazonS3;
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(file.getContentType());
meta.setContentLength(file.getSize());
meta.setHeader("filename", fileName);
ByteArrayInputStream bis = new ByteArrayInputStream(file.getBytes());
TransferManager transferManager = new TransferManager(this.amazonS3);
transferManager.upload(bucket, filename, bis, meta);
That's how I solved it. Hope this help someone. :)
Upvotes: 4
Reputation: 12876
Presumably you are sending this data back to a browser(?). If you are sending the contents of the file back to a browser, you need to set the content-type of the response to indicate to the browser that the file is an image; to do this, you'll want to set the content-type to "image/jpeg".
By specifying the content-type, the browser then knows what to do with the file (in this case, to render the image in the browser).
Upvotes: 0