how to upload the request file to another url

798 views
Skip to first unread message

fuch...@gmail.com

unread,
Apr 25, 2017, 9:28:09 AM4/25/17
to aio-libs
Hey,

suppose the server got a big uploaded file from the client, and the server forward the uploaded file to another url with some extra data.

the handler:

async def test(request):
    mr = await request.multipart()
    post = {}
    while True:
        part = await mr.next()
        if part is None: break
        if 'file' == part.name:
            data = {'foo': 'bar', 'file': part.read()}
            async with aiohttp.ClientSession() as s:
                async with s.post(another_url, data = data) as r:
                    return web.json_response(await r.json())

if the file is very big, the handler will use too much memory, I have saw streaming uploads here: http://aiohttp.readthedocs.io/en/stable/client.html 

@aiohttp.streamer
def file_sender(writer, file_name=None):
    with open(file_name, 'rb') as f:
        chunk = f.read(2**16)
        while chunk:
            yield from writer.write(chunk)
            chunk = f.read(2**16)

# Then you can use `file_sender` as a data provider:

async with session.post('http://httpbin.org/post',
                        data=file_sender(file_name='huge_file')) as resp:
    print(await resp.text())

but how can i add some extra fields in the data?

Nikolay Kim

unread,
Apr 25, 2017, 10:46:26 PM4/25/17
to fuch...@gmail.com, aio-libs
You can construct FormData object, individual multipart items could be part of this form data object. Then you pass form data instance to client call as data parameter

--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+u...@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/724cc94d-79b9-4d06-8f62-8a158abca599%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fuch...@gmail.com

unread,
Apr 26, 2017, 9:02:12 AM4/26/17
to aio-libs, fuch...@gmail.com
thanks kim, is there any way to write raw stream multipart request with multiple params for aiohttp client?

fuch...@gmail.com

unread,
Apr 26, 2017, 4:23:07 PM4/26/17
to aio-libs, fuch...@gmail.com
It's something like http://aiohttp.readthedocs.io/en/stable/client.html#streaming-uploads but streaming upload multiple files. how can i do that with aiohttp? thanks


On Wednesday, April 26, 2017 at 10:46:26 AM UTC+8, Nikolay Kim wrote:

Nikolay Kim

unread,
Apr 26, 2017, 4:26:13 PM4/26/17
to fuch...@gmail.com, aio-libs
Something like this should work

  session.post(“some url”, data=aiohttp.FormData({‘filename’: open(f1), ‘filename2’: open(f2)})

FormData can accept items from request.multipart()


hasan...@gmail.com

unread,
Jul 10, 2018, 8:04:23 AM7/10/18
to aio-libs

Andrew Svetlov

unread,
Jul 12, 2018, 9:48:07 AM7/12/18
to fuch...@gmail.com, aio-libs
Reply all
Reply to author
Forward
0 new messages