谷歌云存储客户端库-代理后面-使用python代码访问存储桶对象? [英] google cloud storage client library - behind proxy - access bucket objects using python code?

查看:68
本文介绍了谷歌云存储客户端库-代理后面-使用python代码访问存储桶对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vmware上使用Centos Linux-gsutil正在运行,但我正在尝试使用python代码从Google云存储下载对象.由于我在代理服务器后面,因此无法在python代码下运行.我尝试导出http_proxy和https_proxy,也通过.boto添加了它(尽管我猜只有gsutil使用它).但是没有办法.

Centos Linux on vmware - gsutil is working but I am trying to download objects from google cloud storage using python code. Running below python code fails as I am behind a proxy server. I tried exporting http_proxy and https_proxy, also adding it via .boto (though i'm guessing only gsutil uses it). But none works.

我也找不到文档中有关代理设置的任何内容.

I can't find any mention of proxy settings in the documentation as well.

from google.cloud import storage

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

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

OSError: [Errno 101] Network is unreachable

*更新17年7月24日-已解决*

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

re-installed google cloud storage library and my script works fine with HTTP_PROXY set in the env. Not sure about the root cause for initial troubles and I was not able to reproduce the error again unfortunately.

推荐答案

不过, python库不直接支持代理,它使用HTTPS_PROXY环境变量(如果已设置).

Although, google-cloud python library does not have support for proxies directly, it honors HTTPS_PROXY environment variable if set.

要么:

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脚本中设置此设置(最好是在开始时):

You could also set this directly within your python script too (preferably at the very beginning):

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,urllib模块中支持https_proxy,因此任何使用urllib的库(例如此处的google-cloud)都可以透明地将请求用作代理.

BTW, https_proxy is supported in the urllib module and hence any libraries (like google-cloud here) using urllib can transparently use the proxies for the requests.

这篇关于谷歌云存储客户端库-代理后面-使用python代码访问存储桶对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆