You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to implement a POST request to upload large files (~4GB) using connexion set up with a Flask server. The physical is an embedded one so the performances are critical.
Having read the OpenAPI 3.0 File Upload specs, it suggests to use multipart/form-data to upload a file with additional data (e.g filename, user ID, ...). Here is the API I'm using :
api.yaml
paths:
/records:
post:
tags:
- recordssummary: Upload a new record to the serveroperationId: server.controllers.file_controller.records_postrequestBody:
required: truecontent:
multipart/form-data:
schema:
additionalProperties: falsetype: objectproperties:
record:
type: stringformat: binarydescription: Record to be uploaded to the serverfilename:
type: stringdescription: New filename for the record being uploadedrequired:
- recordrequired:
- recordresponses:
201:
description: Created, the new record has been successfully uploaded to the server409:
description: Conflict, a record already exists with that name on the server
file_controller.py
defrecords_post():
# Create destination directory if it does not existos.makedirs(os.path.dirname(RECORDS_PATH), exist_ok=True)
record=connexion.request.files['record']
filename=secure_filename(record.filename)
if'filename'inconnexion.request.formandconnexion.request.form['filename']:
filename=secure_filename(connexion.request.form['filename'])
filepath=os.path.join(RECORDS_PATH, filename)
ifos.path.isfile(filepath):
return {'status': 'Conflict', 'message': 'A record already exists with that name on the server'}, 409try:
record.save(filepath)
exceptExceptionase:
logging.error("Failed to save '{}' to: {}".format(filename, RECORDS_PATH))
logging.error(str(e))
return {'status': 'Internal Server Error', 'message': 'Failed to upload record to the server',
'error': str(e)}, 500return {'status': 'Created', 'message': 'Record uploaded successfully !', 'location': filename}, 201
When using multipart/form-data I can reach to the uploaded file using connexion.request.files['record'] but it seems that it has to save the file into disk when file size exceeds 500KB and I'm limited to the max available space in /tmp on my server (which is ~1.8GB).
If I use application/octet-stream, I can stream the file directly but I'm loosing any chance to upload additional data and I encounter the same problem as above which is: connexion.request.stream is empty anyway and I can't read connexion.request.get_data() into chunks.
Is there any way to read non-empty connexion.request.stream or read connexion.request.get_data() into chunks ?
Additional info:
Output of the commands:
python --version3.8.5
pip show connexion | grep "^Version\:"2.7.0
OpenAPI version : 3.0.0
The text was updated successfully, but these errors were encountered:
Description
I'm trying to implement a POST request to upload large files (~4GB) using connexion set up with a Flask server. The physical is an embedded one so the performances are critical.
Having read the OpenAPI 3.0 File Upload specs, it suggests to use
multipart/form-data
to upload a file with additional data (e.g filename, user ID, ...). Here is the API I'm using :api.yaml
file_controller.py
When using
multipart/form-data
I can reach to the uploaded file usingconnexion.request.files['record']
but it seems that it has to save the file into disk when file size exceeds 500KB and I'm limited to the max available space in/tmp
on my server (which is ~1.8GB).Some people in the issues suggest to use streaming-form-data, so I've taken a look at an implementation for Flask server but it seems that
connexion.request.stream
is empty. Apparently, the stream would be empty if it was previously read by request.data... and if I userequest.data
orrequest.get_data()
, I can't find a way to split the bytestring into chunks to save memory and write to the right destination.If I use
application/octet-stream
, I can stream the file directly but I'm loosing any chance to upload additional data and I encounter the same problem as above which is:connexion.request.stream
is empty anyway and I can't readconnexion.request.get_data()
into chunks.Is there any way to read non-empty
connexion.request.stream
or readconnexion.request.get_data()
into chunks ?Additional info:
Output of the commands:
python --version
3.8.5pip show connexion | grep "^Version\:"
2.7.0The text was updated successfully, but these errors were encountered: