return 在 REST 控制器中包装 S3Object.getObjectContent() 的 ResponseEntity<InputStreamResource> 是否安全?
Is it safe to return an ResponseEntity<InputStreamResource> that wraps S3Object.getObjectContent() in REST controller?
我正在开发一个 Spring 引导应用程序,它应该允许用户通过指定的应用程序 REST 接口从 Amazon S3 间接下载文件。为此,我有一个 REST 控制器,它 return 向用户提供一个 InputStreamResource,如下所示:
@GetMapping(path = "/download/{fileId}")
public ResponseEntity<InputStreamResource> downloadFileById(@PathVariable("fileId") Integer fileId) {
Optional<LocalizedFile> fileForDownload = fileService.getConcreteFileForDownload(fileId);
if (!fileForDownload.isPresent()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileForDownload.get().getFilename())
.body(new InputStreamResource(fileService.download(fileForDownload.get())));
}
文件服务中的下载方法如下所示:
@Override
public InputStream download(LocalizedFile file) {
S3Object obj = s3client.getObject(bucketName, file.getFilename());
return obj.getObjectContent();
}
我担心的是来自 Amazon SDK 的输入流无法在控制器中明确关闭。有以下 warning in AWS documentation of the getObjectContent() method,这让我怀疑我的上述方法是否成功:
If you retrieve an S3Object, you should close this input stream as
soon as possible, because the object contents aren't buffered in
memory and stream directly from Amazon S3. Further, failure to close
this stream can cause the request pool to become blocked.
因此我的问题是:
在控制器中 return 和 ResponseEntity<InputStreamResource>
安全吗?这个来自S3Object.getObjectContent()
的InputStream会不会在下载成功后自动关闭?到目前为止,我的方法已经成功,但我不太确定未来可能的后果。
经过一些研究,我发现 ,这应该适用于我的问题。
Tl;dr 版本:给定输入流的 Spring MVC handles the closing,所以我上面描述的方法应该是安全的。
我正在开发一个 Spring 引导应用程序,它应该允许用户通过指定的应用程序 REST 接口从 Amazon S3 间接下载文件。为此,我有一个 REST 控制器,它 return 向用户提供一个 InputStreamResource,如下所示:
@GetMapping(path = "/download/{fileId}")
public ResponseEntity<InputStreamResource> downloadFileById(@PathVariable("fileId") Integer fileId) {
Optional<LocalizedFile> fileForDownload = fileService.getConcreteFileForDownload(fileId);
if (!fileForDownload.isPresent()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileForDownload.get().getFilename())
.body(new InputStreamResource(fileService.download(fileForDownload.get())));
}
文件服务中的下载方法如下所示:
@Override
public InputStream download(LocalizedFile file) {
S3Object obj = s3client.getObject(bucketName, file.getFilename());
return obj.getObjectContent();
}
我担心的是来自 Amazon SDK 的输入流无法在控制器中明确关闭。有以下 warning in AWS documentation of the getObjectContent() method,这让我怀疑我的上述方法是否成功:
If you retrieve an S3Object, you should close this input stream as soon as possible, because the object contents aren't buffered in memory and stream directly from Amazon S3. Further, failure to close this stream can cause the request pool to become blocked.
因此我的问题是:
在控制器中 return 和 ResponseEntity<InputStreamResource>
安全吗?这个来自S3Object.getObjectContent()
的InputStream会不会在下载成功后自动关闭?到目前为止,我的方法已经成功,但我不太确定未来可能的后果。
经过一些研究,我发现
Tl;dr 版本:给定输入流的 Spring MVC handles the closing,所以我上面描述的方法应该是安全的。