In our flask API, we have an endpoint for uploading a document. This endpoint receives the uploaded document, does some checks and uploads this document to MinIO. We want to stream the received document directly to the remote storage without actually processing it via the API.
One idea is to do the following:
@app.route('/upload', methods=['POST'])
def upload():
upload_url = get_presigned_upload_url()
response = requests.put(url=upload_url, files={'file': request.stream})
I can see two problems in the above code:
1- How can I make sure that the stream consists of only one file
2- How can I extract the mimetype from the actual file, without just trusting the file extension
Following the suggestion in here for uploading large files, I was able to get all the info that I need including mimetype and number of files uploaded.
The code that I have used:
@app.route('/upload', methods=['POST'])
def upload():
upload_url = get_presigned_upload_url()
def custom_stream_factory(total_content_length, filename, content_type, content_length=None):
import tempfile
tmpfile = tempfile.NamedTemporaryFile('wb+', prefix='flaskapp', suffix='.nc')
return tmpfile
import werkzeug, flask
stream, form, files = werkzeug.formparser.parse_form_data(flask.request.environ,
stream_factory=custom_stream_factory)
files_list = []
for fil in files.values():
files_list.append(
('file', (fil.filename, fil, fil.mimetype))
)
break
response = requests.put(url=upload_url, files=files_list)