GAE - urlfetch multipart post not working with lar

2019-07-26 04:57发布

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条回答
乱世女痞
2楼-- · 2019-07-26 05:23

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).

查看更多
登录 后发表回答