I am working on an API (spring boot). In which i need to return FileInputStream in Response body of get method.
Expected - When front end call the get-files API, a file download prompt should be open on browser.
Problem - We can't use blob.downloadToFile method, because it will download the file on local machine(or where APIs are hosted) and we need something using which we can send the File directly to front-end on API call. However there is another method blob.download(), which returns OutputStream which can't be returned on API call.
So is there any way that we can convert that OutputStream to FileInputStream without saving to a actual file on our device.
Example code -
public ByteArrayOutputStream downloadBlob() throws URISyntaxException, InvalidKeyException, StorageException, IOException {
String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName + ";"
+ "AccountKey=" + accountKey;
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
final CloudBlobContainer container = blobClient.getContainerReference("container name");
CloudBlockBlob blob = container.getBlockBlobReference("image.PNG");
ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
blob.download(outputStream);
System.out.println("file downloaded");
return outputStream;
}
Note- the file can be of any type.