在 Spring Boot 中将 AWS S3 文件作为流下载 [英] Downloading AWS S3 file as a stream in Spring boot
问题描述
我想公开一个 API,以将 S3 存储桶文件内容作为流下载给其使用者.API URL 类似于/downloadfile/** 这是 GET 请求.
I want to expose an API to download a S3 bucket file content as stream to its consumers. The API URL is like /downloadfile/** which is GET request.
- 现在我的返回类型应该是什么 我尝试使用 accept header=应用程序/八位字节流不起作用.
- 我不想将文件的内容写入任何文件并发送.它应该作为流返回,就是这样.
这是我写到现在的控制器伪代码,它一直给我 406 错误.
Here is the controller pseudo code I wrote till now which is giving me 406 error all the time.
@GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<Object> downloadFile(HttpServletRequest request) {
//reads the content from S3 bucket and returns a S3ObjectInputStream
S3ObjectInputStream object = null;
object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log").getObjectContent();
return object
}
这里有什么关于这样做的方法以及我做错了什么的建议吗?
Any suggestions here on the way of doing this and what I am doing wrong?
推荐答案
我能够使用 Spring 的 StreamingResponseBody
类以流的形式下载文件.
I was able to download the file as a stream by using StreamingResponseBody
class from Spring.
这是我使用的代码:
@GetMapping(value = "/downloadfile/**", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<S3ObjectInputStream> downloadFile(HttpServletRequest request) {
//reads the content from S3 bucket and returns a S3ObjectInputStream
S3Object object = publishAmazonS3.getObject("12345bucket", "/logs/file1.log");
S3ObjectInputStream finalObject = object.getObjectContent();
final StreamingResponseBody body = outputStream -> {
int numberOfBytesToWrite = 0;
byte[] data = new byte[1024];
while ((numberOfBytesToWrite = finalObject.read(data, 0, data.length)) != -1) {
System.out.println("Writing some bytes..");
outputStream.write(data, 0, numberOfBytesToWrite);
}
finalObject.close();
};
return new ResponseEntity<>(body, HttpStatus.OK);
}
测试流媒体是否正确完成的方法是下载一个大约 400mb 的文件.通过传入 vm 选项将 Xmx 减少到 256mb.现在,比较使用和不使用 StreamingResponseBody
的下载功能,当使用传统的 OutputStreams 写入内容时,您将得到 OutofMemoryError
The way to test the streaming is done correctly or not is to have a file of around 400mb to download. Reduce your Xmx to 256mb by passing the in the vm options. Now, compare the download functionality with and without using StreamingResponseBody
, you will get OutofMemoryError
when using the conventional OutputStreams for writing the content
这篇关于在 Spring Boot 中将 AWS S3 文件作为流下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!