gpt4 book ai didi

python - GAE - urlfetch 多部分帖子不适用于大文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:21 26 4
gpt4 key购买 nike

我正在尝试将位于 Blobstore 中的文件从 GAE 发布到另一个 HTTP 服务 (virustotal)。

当前代码段适用于小文件(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

当我将此代码段与大文件(18 mb)一起使用时,出现以下异常:

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.

谢谢!

最佳答案

App Engine 的 urlfetch 服务 limits the request size to 10 megabytes 。如果您有付费应用程序,则可以使用套接字(我建议使用 httplib 来简化您的代码,但请确保 configure httplib to use sockets instead of urlfetch )。

关于python - GAE - urlfetch 多部分帖子不适用于大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21101854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com