如何在 Java 的 FileInputStream 中获取 azure blob 存储内容?
How to get azure blob storage content in FileInputStream in Java?
我正在开发 API(spring 启动)。其中我需要 return FileInputStream 在 get 方法的响应体中。
预期 -
前端调用get-files时API,浏览器应该会打开文件下载提示
问题 - 我们不能使用 blob.downloadToFile 方法,因为它会将文件下载到本地机器(或托管 API 的地方)我们需要一些东西,我们可以使用它在 API 调用时将文件直接发送到前端。
然而,还有另一种方法 blob.download(),其中 returns OutputStream 不能在 API 调用时被 returned。
那么有什么方法可以将 OutputStream 转换为 FileInputStream 而无需保存到我们设备上的实际文件。
示例代码-
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;
}
注意-文件可以是任何类型。
这是解决方案 - 在浏览器中点击 API 后,您的文件将在浏览器中下载 -
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);
}
}
我正在开发 API(spring 启动)。其中我需要 return FileInputStream 在 get 方法的响应体中。
预期 - 前端调用get-files时API,浏览器应该会打开文件下载提示
问题 - 我们不能使用 blob.downloadToFile 方法,因为它会将文件下载到本地机器(或托管 API 的地方)我们需要一些东西,我们可以使用它在 API 调用时将文件直接发送到前端。 然而,还有另一种方法 blob.download(),其中 returns OutputStream 不能在 API 调用时被 returned。
那么有什么方法可以将 OutputStream 转换为 FileInputStream 而无需保存到我们设备上的实际文件。
示例代码-
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;
}
注意-文件可以是任何类型。
这是解决方案 - 在浏览器中点击 API 后,您的文件将在浏览器中下载 -
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);
}
}