Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming large files using request lib #71

Open
mile95 opened this issue Jan 14, 2021 · 0 comments
Open

Streaming large files using request lib #71

mile95 opened this issue Jan 14, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@mile95
Copy link
Contributor

mile95 commented Jan 14, 2021

In the future, one can imagine that we would like to stream large files to a manager. When sending large files using the request lib (without streaming) is it very likely that python (the program) runs out of memory and we get a MemoryError. To avoid this one can use the requests_toolbelt package for streaming files in the request.post method.

Here is one working example where we stream the content of a file:

from requests_toolbelt import MultipartEncoder

payload = {'field0': "value0", 'field1': "value1"}
files = {'file': ("filename", open('mylargefile.tar', 'rb'), 'application/octet-stream')}

m = MultipartEncoder(fields=dict(files, **payload))
headers = get_base_headers() # Get the required headers.
headers['content-type'] = m.content_type 

response = request.post(
    "url",
     data=m,
     headers=headers
)

In the endpoint, using file: UploadFile in the argument will get the whole streamed file.

@OskarLiew OskarLiew transferred this issue from another repository Apr 20, 2021
@OskarLiew OskarLiew transferred this issue from vikinganalytics/daeploy-issues Dec 9, 2021
@OskarLiew OskarLiew added the enhancement New feature or request label Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants