使用带有boto的服务帐户通过gcs_oauth2_boto_plugin访问GAE中的云存储

时间:2014-08-26 22:15:14

标签: python google-app-engine authentication google-cloud-storage boto

我想知道是否有人知道使用服务帐户进行身份验证的方法,如果我想通过以下方式访问云存储中的数据: 1.使用boto库(和gcs_oauth2_boto_plugin) 2.在Google App Engine(GAE)中运行

关注https://developers.google.com/storage/docs/gspythonlibrary我正在使用boto和gcs_oauth2_boto_plugin对云存储进行身份验证并执行操作(上传/下载文件)。我正在使用服务帐户进行身份验证,以便我们不必定期对Google帐户进行身份验证(我们认为,如果我们在GCE中运行此帐户,它将与GCE服务帐户一起运行 - 避风港& #39;实际上已经完成了)。在本地,我已经设置了我的boto配置文件以使用服务帐户并指向p12密钥文件。这在当地运行良好。

我想使用相同的代码与Google App Engine(GAE)中的云存储进行交互。我们正在运行一个轻量级ETL过程,该过程将数据转换并加载到Big Query中。我们希望在App Engine任务队列中运行此代码(该任务将由来自云存储的对象更改通知触发)。

由于我们当前依赖于boto配置(〜/ .boto),因此我调整了http://thurloat.com/2010/06/07/google-storage-and-app-engine来为服务帐户添加相关配置项。

当我最终从App Engine(dev_appserver.py)运行代码时,我得到以下堆栈跟踪:

Traceback (most recent call last):
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/some-user/dev/myApp/main.py", line 247, in post
    gs.download(fname, fp)
  File "/home/some-user/dev/myApp/cloudstorage.py", line 107, in download
    bytes = src_uri.get_key().get_contents_to_file(fp)
  File "/home/some-user/dev/myApp/boto/storage_uri.py", line 336, in get_key
    bucket = self.get_bucket(validate, headers)
  File "/home/some-user/dev/myApp/boto/storage_uri.py", line 181, in get_bucket
    conn = self.connect()
  File "/home/some-user/dev/myApp/boto/storage_uri.py", line 140, in connect
    **connection_args)
  File "/home/some-user/dev/myApp/boto/gs/connection.py", line 47, in __init__
    suppress_consec_slashes=suppress_consec_slashes)
  File "/home/some-user/dev/myApp/boto/s3/connection.py", line 190, in __init__
    validate_certs=validate_certs, profile_name=profile_name)
  File "/home/some-user/dev/myApp/boto/connection.py", line 568, in __init__
    host, config, self.provider, self._required_auth_capability())
  File "/home/some-user/dev/myApp/boto/auth.py", line 929, in get_auth_handler
    ready_handlers.append(handler(host, config, provider))
  File "/home/some-user/dev/myApp/gcs_oauth2_boto_plugin/oauth2_plugin.py", line 56, in __init__
    cred_type=oauth2_client.CredTypes.OAUTH2_SERVICE_ACCOUNT)
  File "/home/some-user/dev/myApp/gcs_oauth2_boto_plugin/oauth2_helper.py", line 48, in OAuth2ClientFromBotoConfig
    token_cache = oauth2_client.FileSystemTokenCache()
  File "/home/some-user/dev/myApp/gcs_oauth2_boto_plugin/oauth2_client.py", line 175, in __init__
    tempfile.gettempdir(), 'oauth2_client-tokencache.%(uid)s.%(key)s')
  File "/home/some-user/google-cloud-sdk/platform/google_appengine/google/appengine/dist/tempfile.py", line 61, in PlaceHolder
    raise NotImplementedError("Only tempfile.TemporaryFile is available for use")
NotImplementedError: Only tempfile.TemporaryFile is available for use

看起来问题只是gcs_oauth2_boto_plugin在缓存oauth凭据时尝试使用临时目录(App Engine仅支持tempfile.TemporaryFile)。

不是尝试修补gcs_oauth2_boto_plugin,还有其他解决方案吗?我们可以在App Engine上使用gcs_oauth2_boto_plugin / boto的服务帐户来访问云存储资源吗?

或者,我在这里使用了错误的身份验证方法吗?

1 个答案:

答案 0 :(得分:0)

这不能直接回答这个问题,但我没有使用boto和gcs_oauth2_boto_plugin,而是使用“Google云存储” Python客户端库“,来自pip的GoogleAppEngineCloudStorageClient

https://developers.google.com/appengine/docs/python/googlecloudstorageclient/