Calculate md5 from werkzeug.datastructures.FileStorage without saving the object as file

flask, python

Solution

`request.files['file']` is of type `FileStorage` which has a `read()` method. Try doing:

file = request.files['file']

#file.read() is the same as file.stream.read()
img_key = hashlib.md5(file.read()).hexdigest() 

More info on `FileStorage`: http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage

Problem

I'm using Flask for uploading files. In order to prevent storing same file twice, I'm intending to calculate md5 from the file content, and store the file as . unless the file is already there. ``` @app.route('/upload', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': file = request.files['file'] #next line causes exception img_key = hashlib.md5(file).hexdigest() ``` Unfortunatelly, hashlib.md5 throws the exception: ``` TypeError: must be string or buffer, not FileStorage ``` I've already tried file.stream - same effect. Is there any way to get md5 from the file without saving it temporarily?

Original source