【问题标题】:Access google cloud storage using the python api and private key - Bad Handshake使用 python api 和私钥访问谷歌云存储 - 糟糕的握手
【发布时间】:2017-11-24 09:17:02
【问题描述】:

我正在尝试使用 python google.cloud.storage api 访问我的谷歌存储桶,但出现异常:

requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')],)",)

我在 GCP 中创建了一个服务帐户,将其定义为项目所有者和编辑者、存储对象创建者和查看者。我还将它添加为特定的存储桶对象管理员、创建者和查看者。我通过凭证页面为此帐户创建了一个私钥 (json),我尝试将其与 python 客户端一起使用。这些都不起作用。 下面附上python代码:

from google.cloud import storage
client = storage.Client.from_service_account_json('service_account_private_key.json')
bucket = client.get_bucket('bucket-name')
blob = bucket.get_blob('text.txt')
print(blob.download_as_string())

【问题讨论】:

  • 您是否位于拦截 HTTPS 流量的防火墙后面?
  • 据我所知,我是从 GCP 计算引擎实例上的 docker 运行的。有趣的是 gsutil 在同一个 docker 实例中工作。

标签: python ssl ssl-certificate google-cloud-storage


【解决方案1】:

您的证书似乎有问题。您可能可以使用以下代码重现它:

import requests
requests.get(
    # Arbitrary Google API URL
    'https://www.googleapis.com/discovery/v1/apis/storage/v1/rest')

很可能,您的系统要么不信任签署 Google 密钥的证书颁发机构(不太可能),要么您位于防火墙后面,该防火墙正在用自己的证书替换真实证书(很可能)。您可以尝试禁用您不想在生产中执行的证书检查,看看是否是问题所在:

import requests
requests.get(
    'https://www.googleapis.com/discovery/v1/apis/storage/v1/rest',
    verify=False)

【讨论】:

  • 你是对的,禁用证书检查有效。我从 GCP 实例上的 docker 运行,试图访问同一项目上的谷歌存储。不知道我应该改变什么才能让它发挥作用。
  • 更新:我启动了一个新的 gcp 引擎实例,允许默认服务帐户具有对存储的完全访问权限,gsutil 在 docker 内工作,但 python api 仍然无法使用默认凭据.
猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2017-11-23
  • 2018-05-11
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多