在需要登录时使用 python 从 cloud.google.com 下载 public 个文件

Download public files from cloud.google.com using python when it requires login

我正在尝试下载 Twitter misinformation/elections-integrity 数据集,网址为: https://storage.cloud.google.com/twitter-election-integrity/hashed/ira/ira_media_file_list_hashed.txt

但是需要登录。我没有使用 Google App Engine,只是在我的笔记本电脑上使用 python 3 运行。我编写了以下代码来下载文件:

for a_url in download_urls:
    filename = os.path.join(data_path, os.path.basename(a_url))

    if not os.path.isfile(filename):
        #urllib.request.urlretrieve(a_url, filename)
        with open(filename, 'wb') as f:
            c = pycurl.Curl()
            c.setopt(c.URL, a_url)
            c.setopt(c.WRITEDATA, f)
            c.setopt(c.CAINFO, certifi.where())
            c.perform()
            c.close()

有没有一种方法可以下载这些文件,同时避免登录我的 google 帐户?

或者是否有通过 python 登录的简便方法?

几乎所有在线信息都是如何在 GAE 环境中执行此操作的,我并没有尝试连接到存储桶。

提到的 URL 表示文件来自 Cloud Storage. Since logging in is required it means the objects aren't publicly accessible

提供这些文件的应用程序使用以用户为中心的 OAuth 2.0 流程。来自 Authentication:

Cloud Storage uses OAuth 2.0 for API authentication and authorization. Authentication is the process of determining the identity of a client.

  • A user-centric flow allows an application to obtain credentials from an end user. The user signs in to complete authentication.

Is there a way I can download these files while avoiding having to log in to my google account?

这里的答案应该是。否则就是一个错误 - 你可以绕过 Google 云安全 ;)

我找不到 pycurl 的具体信息,但 curl 本身并未列出支持的 OAuth 2.0。来自 Features -- what can curl do:

HTTP

  • authentication: Basic, Digest, NTLM (*9) and Negotiate (SPNEGO) (*3) to server and proxy

所以我认为您将无法使用 pycurl 下载文件。至少不是直接的(也许通过代理?)。

一种可能的替代方法是在脚本中使用 Cloud SDK's gsutil(作为任何其他外部进程启动)。:

  • 您首先需要使用 gcloud auth login.
  • 获取身份验证令牌
  • 然后您将启动您的脚本,其中的 gsutil 执行将使用先前获得的身份验证令牌

我看到可以在独立模式下安装和使用 gsutil,无需云 SDK,但我没有以这种方式使用它。也许值得为您的情况进行调查。来自 gsutil config:

The gsutil config command applies to users who have installed gsutil as a standalone tool.

The gsutil config command obtains access credentials for Google Cloud Storage and writes a boto/gsutil configuration file containing the obtained credentials along with a number of other configuration-controllable values.