Django StreamingHttpResponse to return large files
I need to get pdf files from s3 and return the same file to the frontend.
def stream_pdf_from_s3(request, file_key):
s3_client = boto3.client('s3')
try:
response = s3_client.get_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=file_key)
pdf_stream = response['Body']
# Use iter_chunks() for efficient streaming
return StreamingHttpResponse(pdf_stream.iter_chunks(chunk_size=65,536), content_type='application/pdf')
except Exception as e:
return HttpResponse(f"Error fetching PDF: {e}", status=500)
But in the browser's network tab, it seems that no bytes are streamed even after a longer time. The request is in pending
state.
The expectation was bytes soon be returned in chunks immediately.
What could be the reason and is there any improper configuration with the code?