[Django]-How to download large file from Cloud Run in Django

3👍

The Cloud Run HTTP request/response size is limited to 32Mb. Use a multipart/form-data to send chunks of your big file and not the whole file directly.

2👍

Thank you @guillaume blaquiere! I solved download error. I post my code for othres.

def _file_iterator(file, chunk_size=512):
    with open(file, 'rb') as f:
        while True:
            c = f.read(chunk_size)
            if c:
                yield c
            else:
                break

def download_view(request,pk):
    file_path = f'media/{pk}.mp3'
    file_name = f'{pk}.mp3'
    response = StreamingHttpResponse(_file_iterator(file_path))
    response['Content-Type'] = 'audio/mpeg'
    response['Content-Disposition'] = f'attachment;filename="{file_name}"'
    return response

I think StreamingHttpResponse is key point of this problem. It’s return big file by chunks. It dose not over Cloud Run’s limit.

When I used multipart/form-data for Content-Type, I could download file. But it’s couldn’t open on smart phone, because It couldn’t select application. When I download on PC, it’s can’t show audio file icon. We should select exact content type.

enter image description here

👤Nori

Leave a comment