首页 > 解决方案 > Downloading file using HttpResponse throwing error as "body size is too long" for large files (>5MB)

问题描述

I am retrieving file from s3 bucket and returning content as a file as shown below:

s3 = boto3.resource("s3")     
object = s3.Object(bucket-name,s3_key)    
text = object.get()['Body']        
response = FileResponse(text)          
response['Content-Disposition'] = 'attachment;filename=sample.xls'          
return response 

But i am getting error as "body size is too long" while returning file.

标签: djangopython-3.xfileamazon-s3download

解决方案


尝试:

from django.core.servers.basehttp import FileWrapper
from django.http import FileResponse

s3 = boto3.resource("s3")     
object = s3.Object(bucket-name,s3_key)    
text = object.get()['Body']        
FileResponse(FileWrapper(file(filename, 'rb')), content_type='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=sample.xls'          
return response

这将对您有所帮助。


推荐阅读