使用Python请求在不加载到内存的情况下“桥接”文件?

2024-10-08 19:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Python Requests库从url获取一个文件,并在post请求中将其用作mulitpart编码的文件。问题是文件可能非常大(50MB-2GB),我不想在内存中加载它。(上下文here。)

在文档(multipartstream downstream up)中,我想出了这样的例子:

    with requests.get(big_file_url, stream=True) as f:
        requests.post(upload_url, files={'file': ('filename', f.content)})

但我不确定我做得对。事实上,它抛出的是从回溯中修正的错误:

^{pr2}$

有什么建议吗?在


Tags: 文件内存文档url编码streamherepost
3条回答

肯尼斯·瑞兹的GitHub repo实际上有一个问题。 我遇到了同样的问题(尽管我只是上传一个本地文件),我添加了一个包装器类,它是与请求的不同部分相对应的流的列表,具有一个read()属性,该属性遍历列表并读取每个部分,还获取头的必要值(边界和内容长度):

# coding=utf-8

from __future__ import unicode_literals
from mimetools import choose_boundary
from requests.packages.urllib3.filepost import iter_fields, get_content_type
from io import BytesIO
import codecs

writer = codecs.lookup('utf-8')[3]

class MultipartUploadWrapper(object):

    def __init__(self, files):
        """
        Initializer

        :param files:
            A dictionary of files to upload, of the form {'file': ('filename', <file object>)}
        :type network_down_callback:
            Dict
        """
        super(MultipartUploadWrapper, self).__init__()
        self._cursor = 0
        self._body_parts = None
        self.content_type_header = None
        self.content_length_header = None
        self.create_request_parts(files)

    def create_request_parts(self, files):
        request_list = []
        boundary = choose_boundary()
        content_length = 0

        boundary_string = b'--%s\r\n' % (boundary)
        for fieldname, value in iter_fields(files):
            content_length += len(boundary_string)

            if isinstance(value, tuple):
                filename, data = value
                content_disposition_string = (('Content-Disposition: form-data; name="%s"; ''filename="%s"\r\n' % (fieldname, filename))
                                            + ('Content-Type: %s\r\n\r\n' % (get_content_type(filename))))

            else:
                data = value
                content_disposition_string =  (('Content-Disposition: form-data; name="%s"\r\n' % (fieldname))
                                            + 'Content-Type: text/plain\r\n\r\n')
            request_list.append(BytesIO(str(boundary_string + content_disposition_string)))
            content_length += len(content_disposition_string)
            if hasattr(data, 'read'):
                data_stream = data
            else:
                data_stream = BytesIO(str(data))

            data_stream.seek(0,2)
            data_size = data_stream.tell()
            data_stream.seek(0)

            request_list.append(data_stream)
            content_length += data_size

            end_string = b'\r\n'
            request_list.append(BytesIO(end_string))
            content_length += len(end_string)

        request_list.append(BytesIO(b'--%s--\r\n' % (boundary)))
        content_length += len(boundary_string)

        # There's a bug in httplib.py that generates a UnicodeDecodeError on binary uploads if
        # there are *any* unicode strings passed into headers as part of the requests call.
        # For this reason all strings are explicitly converted to non-unicode at this point.
        self.content_type_header = {b'Content-Type': b'multipart/form-data; boundary=%s' % boundary}
        self.content_length_header = {b'Content-Length': str(content_length)}
        self._body_parts = request_list

    def read(self, chunk_size=0):
        remaining_to_read = chunk_size
        output_array = []
        while remaining_to_read > 0:
            body_part = self._body_parts[self._cursor]
            current_piece = body_part.read(remaining_to_read)
            length_read = len(current_piece)
            output_array.append(current_piece)
            if length_read < remaining_to_read:
                # we finished this piece but haven't read enough, moving on to the next one
                remaining_to_read -= length_read
                if self._cursor == len(self._body_parts) - 1:
                    break
                else:
                    self._cursor += 1
            else:
                break
        return b''.join(output_array)

因此,不是传递'files'关键字arg,而是将此对象作为'data'属性传递给请求。请求对象

编辑

我已经清理了密码

正如其他答案已经指出的:^{} doesn't support POSTing multipart-encoded files without loading them into memory。在

要上载大文件而不使用多部分/表单数据将其加载到内存中,可以使用^{}

#!/usr/bin/env python
import sys
from urllib2 import Request, urlopen

from poster.encode import multipart_encode # $ pip install poster
from poster.streaminghttp import register_openers

register_openers() # install openers globally

def report_progress(param, current, total):
    sys.stderr.write("\r%03d%% of %d" % (int(1e2*current/total + .5), total))

url = 'http://example.com/path/'
params = {'file': open(sys.argv[1], "rb"), 'name': 'upload test'}
response = urlopen(Request(url, *multipart_encode(params, cb=report_progress)))
print response.read()

它可以调整为允许使用GET response对象而不是本地文件:

^{pr2}$

此解决方案要求GET响应中有一个有效的Content-Length头(已知文件大小)。如果文件大小未知,则分块传输编码可用于上载多部分/表单数据内容。类似的解决方案可以使用urllib3.filepost实现,该方案与requests库一起提供,例如,基于@AdrienF's answer,而不使用poster。在

在python中,您不能将任何需要的内容转换为上下文管理器。它需要非常具体的属性。使用当前代码,可以执行以下操作:

response = requests.get(big_file_url, stream=True)

post_response = requests.post(upload_url, files={'file': ('filename', response.iter_content())})

使用iter_content将确保文件永远不在内存中。将使用迭代器,否则通过使用content属性,文件加载到内存中。在

编辑唯一合理的方法是使用chunk-encoded uploads,例如

^{pr2}$

如果您绝对需要进行多部分/表单数据编码,那么您必须创建一个抽象层,它将在构造函数中使用生成器,以及来自responseContent-Length报头(为len(file)提供答案)将具有从生成器读取的read属性。问题再次是,我很确定在上传之前,整个东西都会被读入内存。在

编辑2

您可以自己制作一个生成器,自己生成multipart/form-data编码的数据。您可以通过与分块编码请求相同的方式传递它,但必须确保您设置了自己的Content-Type和{}头。我没有时间画一个例子,但应该不会太难。在

相关问题 更多 >

    热门问题