谷歌云存储客户端库 - 代理后面 - 使用python代码访问存储桶对象?

时间:2017-07-18 17:29:01

标签: python-3.x proxy google-cloud-storage

Centos Linux on vmware - gsutil正在运行,但我正在尝试使用python代码从谷歌云存储中下载对象。在python代码下面运行失败,因为我在代理服务器后面。我尝试导出http_proxy和https_proxy,也通过.boto添加它(虽然我猜测只有gsutil使用它)。但都没有效果。

我也无法在文档中找到任何代理设置。

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('my-bucket')
blobs=bucket.list_blobs()

OSError:[Errno 101]网络无法访问

*更新24-Jul-17 - 已解决*

  

重新安装谷歌云存储库,我的脚本运行正常   在env中设置HTTP_PROXY。不确定的根本原因   最初的麻烦,我无法再次重现错误   不幸的是

2 个答案:

答案 0 :(得分:1)

尽管google-cloud python library没有直接支持代理,但如果设置了它,则会尊重HTTPS_PROXY环境变量。

或者:

export HTTPS_PROXY=https://mycustomproxy.example.com:12345
python your_python_script.py

或者:

export https_proxy=https://mycustomproxy.example.com:12345
python your_python_script.py

您也可以直接在python脚本中设置它(最好是在开头):

import os
os.environ['https_proxy'] = 'https://mycustomproxy.example.com:12345'

from google.cloud import storage
storage_client = storage.Client()
bucket = storage_client.get_bucket('my-bucket')
blobs=bucket.list_blobs()

BTW,https_proxy模块支持urllib,因此使用google-cloud的任何库(如此处urllib)都可以透明地使用代理进行请求。

答案 1 :(得分:0)

google-cloud python library不支持代理。 gsutil的代理支持来自于它对boto库的使用,因此如果需要代理支持,可以考虑使用该库。