我已经编写了一个Grails应用程序,该应用程序连接到S3并将文件流式传输回客户端。到目前为止,这一直很好,直到我尝试使用它来下载大文件(2GB)为止。我看到以下行为:
在两种情况下,错误消息都是相同的:
errors.GrailsExceptionResolver SocketException occurred when processing request: [GET] /download
Connection reset.:
java.net.SocketException: Connection reset
errors.GrailsExceptionResolver IllegalStateException occurred when processing request: [GET] /download
getOutputStream() has already been called for this response.
这是与下载有关的 Controller 部分:
下载Controller.groovy
def index() {
def fileStream = s3Service.getStream()
response.setHeader("Content-disposition", "attachment;filename=foo.csv")
response.contentType = "text/csv"
response.outputStream = fileStream
response.outputStream.flush()
}
..以及连接到S3并获取文件的服务的摘要:
S3Service.groovy
def getStream() {
def outputStream = ""
try {
AmazonS3 s3 = new AmazonS3Client()
S3Object object = s3.getObject(new GetObjectRequest('my-bucket-name', 'path/to/file.csv'))
outputStream = object.getObjectContent()
}
catch (AmazonServiceException ase) {
/* Log the error. Omitted for brevity. */
}
catch (AmazonClientException ase) {
/* Log the error. Omitted for brevity. */
}
return outputStream
}
我真的很困惑是什么原因造成的。
最佳答案
事实证明,发生此错误是因为该应用程序正在负载平衡器后面的服务器上运行,该服务器正在尝试缓存通过它的文件。由于磁盘空间有限,因此无法缓存较大的文件,并且下载失败。
通过将本地运行的应用程序实例连接到AWS并观察到不再发生奇怪的行为来验证这一点。
关于grails - 为什么此Grails下载行为异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858802/