【问题标题】:authentification for Google Storage using Python使用 Python 对 Google Storage 进行身份验证
【发布时间】:2016-02-03 05:15:00
【问题描述】:

我想构建一个与谷歌存储轻松交互的应用程序,即列出存储桶中的文件、下载文件和上传文件。

this tutorial 之后,我决定使用服务帐户(不是用户帐户)进行身份验证并按照程序进行操作。我在我的控制台上创建了一个公钥/私钥并在我的机器上下载了这个密钥。然后我创建了指向这个私钥的 .boto 文件,最后启动了这个程序并且它工作了:

import boto
import gcs_oauth2_boto_plugin


uri = boto.storage_uri('txxxxxxxxxxxxxx9.appspot.com', 'gs')

for obj in uri.get_bucket():
  print '%s://%s/%s' % (uri.scheme, uri.bucket_name, obj.name)

如您所见,代码中没有使用包gcs_oauth2_boto_plugin,所以我决定去掉它。但神奇的是,当我注释 import gcs_oauth2_boto_plugin 行并再次运行程序时,我得到了这个错误:

C:\Users\...\Anaconda3\envs\snakes\python.exe C:/Users/.../Dropbox/Prog/s3_manifest_builder/test.py
Traceback (most recent call last):
  File "C:/Users/.../Dropbox/Prog/s3_manifest_builder/test.py", line 10, in <module>
    for obj in uri.get_bucket():
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\storage_uri.py", line 181, in get_bucket
    conn = self.connect()
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\storage_uri.py", line 140, in connect
    **connection_args)
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\gs\connection.py", line 47, in __init__
    suppress_consec_slashes=suppress_consec_slashes)
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\s3\connection.py", line 190, in __init__
    validate_certs=validate_certs, profile_name=profile_name)
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\connection.py", line 569, in __init__
    host, config, self.provider, self._required_auth_capability())
  File "C:\Users\...\Anaconda3\envs\snakes\lib\site-packages\boto\auth.py", line 987, in get_auth_handler
    'Check your credentials' % (len(names), str(names)))
boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials

所以我的问题是:

1- 你如何解释删除代码中未使用的导入会导致失败?

2- 更一般地说,为了确保了解身份验证过程,如果我想在机器上运行我的应用程序,我必须确保拥有之前生成的 .boto 文件(指向我的服务帐户私钥) ?或者是否有一种更简洁/更简单的方法可以让我的应用程序访问 Google 存储以进行输入/输出交互?

例如,当我想使用boto 连接到 S3 存储桶时,我只需将公钥和私钥作为字符串提供给我的程序。我不需要生成 .boto 文件、导入包等...,这使它更易于使用,不是吗?

【问题讨论】:

    标签: python google-oauth google-cloud-storage boto


    【解决方案1】:

    1- 你如何解释删除代码中未使用的导入会导致失败?

    第一个提示是该模块被命名为“插件”,尽管表面上不清楚具体是如何实现的。不过,从直觉上讲,不导入模块会导致这种异常。最初,我认为在导入该模块的 init 期间在全局上执行有状态活动是一种不好的做法。在某些方面,它就是这样,但这只是因为类层次结构在元可编程 python 中是“状态”。

    事实证明(在许多情况下)检查堆栈跟踪的抛出位置 (boto.auth.get_auth_handler()) 是理解问题的关键。

    (评论版本见链接源)

    def get_auth_handler(host, config, provider, requested_capability=None):
        ready_handlers = []
        auth_handlers = boto.plugin.get_plugin(AuthHandler, requested_capability)
        for handler in auth_handlers:
            try:
                ready_handlers.append(handler(host, config, provider))
            except boto.auth_handler.NotReadyToAuthenticate:
                pass
    
        if not ready_handlers:
            checked_handlers = auth_handlers
            names = [handler.__name__ for handler in checked_handlers]
            raise boto.exception.NoAuthHandlerFound(
                'No handler was ready to authenticate. %d handlers were checked.'
                ' %s '
                'Check your credentials' % (len(names), str(names)))
    

    注意对类 AuthHandler 的引用,该类在 boto.auth_handler 中定义。

    所以,你可以看到我们需要查看boto.plugin.get_plugin(AuthHandler, requested_capability)的内容:

    def get_plugin(cls, requested_capability=None):
        if not requested_capability:
            requested_capability = []
        result = []
        for handler in cls.__subclasses__():
            if handler.is_capable(requested_capability):
                result.append(handler)
        return result
    

    所以,很明显,终于,当我们看到gcs_oauth2_boto_plugin.oauth2_plugin 中的类OAuth2Auth 的类定义时,它被声明为boto.auth_handler.AuthHandler 的子类,向boto 发出了它的身份验证能力框架通过以下成员变量:

    capability = ['google-oauth2', 's3']
    

    2- 更一般地说,为了确保了解身份验证过程,如果我想在机器上运行我的应用程序,我必须确保拥有之前生成的 .boto 文件(指向我的服务帐户私钥) ?或者是否有一种更简洁/更简单的方法可以让我的应用程序访问 Google 存储以进行输入/输出交互?

    这有一个更通用的答案:您可以使用 .boto 文件,尽管您也可以使用服务帐户凭据,您甚至可以使用 REST API 并通过 oauth2 流程来获取在授权标头。文档中提供了对云存储进行身份验证的各种方法。您链接的教程/文档显示了一些方法,您已将 .boto 用于另一种方法。您可以阅读有关 Cloud Storage REST API (JSON) here 的信息,也可以阅读有关各种类型的 python oauth2 流 here 的信息。

    【讨论】:

      猜你喜欢
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多