GAE - urlfetch多部分文章不适用于大文件 [英] GAE - urlfetch multipart post not working with large files
本文介绍了GAE - urlfetch多部分文章不适用于大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前的代码片段适用于小文件(1 - 2 mb):
导入base64
导入json
导入日志
from poster.encode导入multipart_encode,MultipartParam
from google.appengine.api import urlfetch
$ b def post_file(url,file_name,file_type,file_size,file_obj,options = dict(),username = None,密码=无):
#输入检查
,如果url为无:
如果file_name为None,则为$ Value:ValueError('url')
:$如果file_type为None,
会提高ValueError('file_name')
如果file_size为None,
将增加ValueError('file_type')
$ b $如果file_obj是None,
会提高ValueError('file_size')
如果选项是None,
将提高ValueError('file_obj')
$ b ValueError异常( '选项' )
logging.debug('准备文件{0}'。format(file_name))
#这是帖子参数部分
options ['file' ] = MultipartParam('file',filename = file_name,filetype = file_type,filesize = file_size,fileobj = file_obj)
data,headers = multipart_encode(options)
logging。 debug('Submit the file to {0}'。format(url))
#授权(可选)
如果用户名不是None并且密码不是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)
返回结果
当我在大文件(18 MB)中使用此代码段时,出现以下异常:
Traceback(最近一次调用最后一次):
文件 C:\程序文件(x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py,第162行,位于_handle_POST
api_response = _execute_request(请求)中。在_execute_request $ b中编码()
文件C:\ Program Files(x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py,第120行$ b make_request()
文件C:\程序文件(x86)\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py,行115,在make_request
request_id)
MakeSyncCall $ b中的文件C:\程序文件(x86)\Google\google_appengine\google\appengine\api\apiproxy_stub.py,第109行$ b'对API调用%s。%s()的请求过大。'%(service,call))
RequestTooLargeError:对API调用urlfetch.Fetch()的请求太大。
谢谢!
解决方案
App Engine的urlfetch服务将请求大小限制为10兆字节。如果您有付费应用,则可以使用套接字(我建议使用httplib来简化您的代码,但请确保配置httplib使用套接字而不是urlfetch )。
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!
解决方案
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).
这篇关于GAE - urlfetch多部分文章不适用于大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文