尝试在appengine上使用clouddatastore库的begin_transaction()函数时出现“DNS lookup failed”错误

时间:2014-08-08 09:50:23

标签: python google-app-engine google-cloud-datastore

我是谷歌云数据存储的新手。我在python中尝试了关于谷歌云数据存储开始的this文档。我在我的终端上做了一个简单的例子。

所以,我尝试在同一项目下运行与谷歌应用引擎上的服务相同的代码,我的云数据存储的计算引擎正在运行。

这是我的代码:

import googledatastore as datastore

def data_store_test(self):
   datastore.set_options(dataset='my-dataset-id')
   # Create a RPC request to begin a new transaction.
   req = datastore.BeginTransactionRequest()
   # Execute the RPC synchronously.
   resp = datastore.begin_transaction(req)

当我在我的应用引擎中运行此代码时,我在代码的最后一行出现了此错误:

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/handlers/test_handler.py", line 49, in data_store_test
datastore.begin_transaction(req)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/googledatastore/__init__.py", line 85, in begin_transaction
return get_default_connection().begin_transaction(request)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/googledatastore/__init__.py", line 66, in get_default_connection
_options['credentials'] = helper.get_credentials_from_env()
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/googledatastore/helper.py", line 70, in get_credentials_from_env
credentials.refresh(http)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/oauth2client/client.py", line 516, in refresh
self._refresh(http.request)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/oauth2client/gce.py", line 82, in _refresh
response, content = http_request(uri)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/oauth2client/util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/oauth2client/client.py", line 475, in new_request
self._refresh(request_orig)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/oauth2client/gce.py", line 82, in _refresh
response, content = http_request(uri)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/httplib2/__init__.py", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/httplib2/__init__.py", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/base/data/home/apps/s~my-dataset-id/0-0-1.377819373230447398/httplib2/__init__.py", line 1286, in _conn_request
response = conn.getresponse()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/gae_override/httplib.py", line 534, in getresponse
'An error occured while connecting to the server: %s' % e)
error: An error occured while connecting to the server: DNS lookup failed for URL: http://metadata.google.internal/0.1/meta-data/service-accounts/default/acquire?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdatastore%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email

我必须在环境变量中设置凭据(DATASTORE_PRIVATE_KEY_FILE,DATASTORE_SERVICE_ACCOUNT)才能使此代码在终端上运行。当我在同一个项目中运行此代码时,我有我的数据存储区,我认为我不必提供这些凭据。

我是否必须通过在app.yaml文件中添加“vm:true”行来在vm实例下运行此应用引擎代码?

我觉得我在这里错过了一个小小的想法,但我不知道它是什么。请帮帮我。

1 个答案:

答案 0 :(得分:0)

要从App Engine应用程序直接访问数据存储,我们建议您使用Python DB Datastore APIPython NDB Datastore API

Cloud Datastore Python API旨在从其他环境(如Google Compute Engine)访问云数据存储区。