GAE - urlfetch multipart post not working with large files

混江龙づ霸主 提交于 2019-12-11 12:09:29

问题


Im trying to post a file located in the Blobstore from GAE to another HTTP service (virustotal).

the current snippet is working for small files (1 - 2 mb):

import base64
import json
import logging
from poster.encode import multipart_encode, MultipartParam
from google.appengine.api import urlfetch


def post_file(url, file_name, file_type, file_size, file_obj, options=dict(), username=None, password=None):

    # Input checks
    if url is None:
        raise ValueError('url')

    if file_name is None:
        raise ValueError('file_name')

    if file_type is None:
        raise ValueError('file_type')

    if file_size is None:
        raise ValueError('file_size')

    if file_obj is None:
        raise ValueError('file_obj')

    if options is None:
        raise ValueError('options')

    logging.debug('Preparing file {0}'.format(file_name))

    # This is the post arguments section
    options['file'] = MultipartParam('file', filename=file_name, filetype=file_type, filesize=file_size, fileobj=file_obj)

    data, headers = multipart_encode(options)

    logging.debug('Submitting the file to {0}'.format(url))

    # For authorization (optional)
    if username is not None and password is not None:
        headers['Authorization'] = generate_authorization_header(username, password)

    fetch = urlfetch.fetch(url=url, payload="".join(data), method=urlfetch.POST, headers=headers)
    response = fetch.content

    result = json.loads(response)

    return result

When i use this snippet with large files (18 mb) i get the following exception:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py", line 162, in _handle_POST
    api_response = _execute_request(request).Encode()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py", line 120, in _execute_request
    make_request()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py", line 115, in make_request
    request_id)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub.py", line 109, in MakeSyncCall
    'The request to API call %s.%s() was too large.' % (service, call))
RequestTooLargeError: The request to API call urlfetch.Fetch() was too large.

Thanks!


回答1:


App Engine's urlfetch service limits the request size to 10 megabytes. If you have a paid app, you can use sockets (I'd recommend using httplib to simplify your code but make sure to configure httplib to use sockets instead of urlfetch).



来源:https://stackoverflow.com/questions/21101854/gae-urlfetch-multipart-post-not-working-with-large-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!