Questions & Answers

How to stream large tar.gz file in flask app?

`Currently, I'm using "send_from_directory" to serve the tar.gz file. It's working good for 200-300 Mb file but if file size more than 300 Mb or 400 Mb it's giving and issue.

`@app.route('/download', methods=['GET','POST'])
def download():
    json_data = request.get_json()
    data =
    if 'file_name' in json_data:
               file_name = json_data['file_name']
               pathdir = json_data['pathdir']
               zipfile = pathdir+'/'+str(file_name)+'.tar.gz'
               if os.path.exists(zipfile):
               tar = tarfile.TarFile.gzopen(zipfile, mode="w")
               tar.add(pathdir, arcname=os.path.basename(pathdir))
               return send_from_directory(pathdir, str(file_name)+'.tar.gz', as_attachment=True)
        except Exception as e:
            logger.exception("Exception occurred in code.")
            return jsonify(error='file_name not found'+str(e))
        return jsonify(message='please provide file_name name.')`
2023-01-25 00:05:03
Welcome to Stack Overflow. Can you please define what "it's giving and issue" means? What behavior are you getting, and what is the desired behavior?
2023-01-25 00:05:03
Are you aware that your code allows you to delete every .tar.gz file on the system if not prevented by the OS.
2023-01-25 00:05:03
If you don't want to have to hold the entire file you are sending in memory but rather want to read the file off of disk as you send send it out as the body of the request, then you can use the lower level flask.send_file function. This function will accept a file pointer, so you can open the file you want to send and then pass the file handle of the open file to this function. In this way, the entire file should never have to be held in memory.
Answers(0) :