Reputation: 532
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.
Upvotes: 3
Views: 9114
Reputation: 532
Here is the solution - After hitting the API in browser, your file will be downloaded in browser -
package com.example.demo.controllers;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@RestController
public class FileDownload {
@GetMapping(path="/download-file")
public ResponseEntity<Resource> getFile(){
String fileName = "Can not find symbol.docx";
//azure credentials
String connection_string = "Connection String of storage account on azure";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connection_string).buildClient();
BlobContainerClient containerClient= blobServiceClient.getBlobContainerClient("Container name");
System.out.println(containerClient.getBlobContainerName());
BlobClient blob = containerClient.getBlobClient(fileName);
//creating an object of output stream to recieve the file's content from azure blob.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
blob.download(outputStream);
//converting it to the inputStream to return
final byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ByteArrayResource resource = new ByteArrayResource(bytes);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.headers(headers)
.body(resource);
}
}
Upvotes: 5