关于python:计算引擎:此环境中不提供URLFetch

Compute Engine: URLFetch is not available in this environment

我试图将一个python服务器放在Google Compute Engine上的VM中,其持续时间超过10分钟(App Engine中的服务时间限制)。 我给你我的代码和输出:

1
2
3
4
5
6
7
8
9
10
from google.cloud import storage as gcs
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()

def my_function(filename, stringdata):

   # init client gcp
   gcs_client = gcs.Client()
   bucket = gcs_client.get_bucket(config["cloud_storage_segment"])
   upload_blob_from_string(bucket, filename, stringdata)

输出说我这个:

File
"/home/guillermo.diaz/.local/lib/python2.7/site-packages/flask/app.py",
line 1994, in call
return self.wsgi_app(environ, start_response)

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1985, in wsgi_app
response = self.handle_exception(e)

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1540, in handle_exception
reraise(exc_type, exc_value, tb)

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1982, in wsgi_app
response = self.full_dispatch_request()

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1612, in full_dispatch_request
rv = self.dispatch_request()

File"/home/user/.local/lib/python2.7/site-packages/flask/app.py",
line 1598, in dispatch_request
return self.view_functionsrule.endpoint

File"/home/user/my_module/main.py",
line 50, in my_function
bucket = gcs_client.get_bucket(config["cloud_storage_segment"])

File
"/home/user/.local/lib/python2.7/site-packages/google/cloud/storage/client.py",
line 225, in get_bucket
bucket.reload(client=self)

File"/home/user/.local/lib/python2.7/site-packages/google/cloud/storage/_helpers.py",
line 108, in reload
_target_object=self)

File"/home/user/.local/lib/python2.7/site-packages/google/cloud/_http.py",
line 290, in api_request
headers=headers, target_object=_target_object)

File"/home/user/.local/lib/python2.7/site-packages/google/cloud/_http.py",
line 183, in _make_request
return self._do_request(method, url, headers, data, target_object) File
"/home/user/.local/lib/python2.7/site-packages/google/cloud/_http.py",
line 211, in _do_request
return self.http.request(

File"/home/user/.local/lib/python2.7/site-packages/google/cloud/_http.py",
line 73, in http
return self._client._http

File"/home/user/.local/lib/python2.7/site-packages/google/cloud/client.py",
line 151, in _http
self._credentials))

File"/home/user/.local/lib/python2.7/site-packages/google/auth/transport/requests.py",
line 161, in init
super(AuthorizedSession, self).init(**kwargs)

File"/home/user/.local/lib/python2.7/site-packages/requests/sessions.py",
line 396, in init
self.mount('https://', HTTPAdapter())

File"/home/user/.local/lib/python2.7/site-packages/requests_toolbelt/adapters/appengine.py",
line 79, in init
super(AppEngineAdapter, self).init(*args, **kwargs)

File"/home/user/.local/lib/python2.7/site-packages/requests_toolbelt/adapters/appengine.py",
line 60, in init
super(AppEngineMROHack, self).init(*args, **kwargs)

File"/home/user/.local/lib/python2.7/site-packages/requests/adapters.py",
line 127, in init
self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block)

File
"/home/user/.local/lib/python2.7/site-packages/requests_toolbelt/adapters/appengine.py",
line 82, in init_poolmanager
self.poolmanager = _AppEnginePoolManager(self._validate_certificate)

File"/home/user/.local/lib/python2.7/site-packages/requests_toolbelt/adapters/appengine.py",
line 122, in init
validate_certificate=validate_certificate)

File"/home/user/.local/lib/python2.7/site-packages/urllib3/contrib/appengine.py",
line 103, in init
"URLFetch is not available in this environment.")

有人可以帮助我吗? 谢谢你的时间!


我解决了这个问题......

当我使用此导入时:

1
2
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()

所有的http请求都尝试使用URLFetch,并在GCE中创建问题。
只有我需要删除这两行,一切都完美。

谢谢你的时间!GDJ