python - How to stream upload a file using multipart/form-data -
i trying hug receive multipart/form-data
post request , stream body in chunks straight disk. able upload stream large binary file using application/octet-stream
post method. here hug method:
@hug.post('/upload',versions=1,requires=cors_support) def upload_file(body,request,response): gunicorn.http.body import body """receives stream of bytes , writes them file name provided in request header.""" # if content-type multipart/form-data, # body dictionary. load # whole thing memory before writing # disk. if type(body) == dict: filename = body['filename'] filebody = body['file'] open(filename,'wb') f: f.write(filebody) # if content-type application/octet-stream # body gunicorn.http.body.body. # file-like object streamed , # written in chunks. elif type(body) == body: filename = request.headers['filename'] chunksize = 4096 open(filename,'wb') f: while true: chunk = body.read(chunksize) if not chunk: break f.write(chunk) return
and here curl snippet:
url=http://localhost:8000/v1/upload filename=largefile.dat curl -v -h "filename: $filename" \ -h "content-type: application/octet-stream" \ --data-binary @$filename -x post $url
the above works , i'm able stream upload file because in upload_file
function, body
gunicorn.http.body.body
instance able stream straight disk in chunks.
however need able upload files browser, sends multipart/form-data
post request. emulate curl, do:
url=http://localhost:8000/v1/upload filename=largefile.dat curl -v -h "filename: $filename" \ -h "content-type: multipart/form-data" \ -f "filename=$filename" \ -f "file=@$filename;type=application/octet-stream" \ -x post $url
this time, in hug, body
dictionary, , body['file']
bytes
instance. don't know how stream disk without loading whole thing in memory first.
is there way obtain body file object stream straight disk?
Comments
Post a Comment