如何通过 HTTP代理路由 BigQuery client 来电?
在发布此内容之前,我尝试过关注但仍未通过http代理进行路由。 Google Cloud服务凭据通过shell环境变量 GOOGLE_APPLICATION_CREDENTIALS
设置import httplib2
import socks
import google.auth
credentials, _ = google.auth.default()
http_client = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'someproxy', 80));
bigquery_client = bigquery.Client(credentials=credentials, _http=http_client)
传出流量(172.217.x.x属于googleapis.com)不通过HTTP代理进行路由,
$ netstat -nputw
Local Address Foreign Address
x.x.x.x 172.217.6.234:443 SYN_SENT
编辑:找到解决方案,将其作为答案发布。
答案 0 :(得分:1)
我发现创建这些凭据的唯一方法是直接在我的操作系统环境中设置它。
假设您已经拥有json credential file,也许这对您有用:
import httplib2
import socks
import os
import google.auth
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/your/credentials_file.json'
credentials, _ = google.auth.default()
http_client = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'someproxy', 80))
bigquery_client = bigquery.Client(_http=http_client, credentials=credentials)
答案 1 :(得分:1)
使用最新的API(google-auth 1.5.0)更新Pratap Koritala's answer:
import os
import google.auth
from google.cloud import bigquery
os.environ["HTTP_PROXY"] = "someproxy:80"
os.environ["HTTPS_PROXY"] = "someproxy:80"
credentials, _ = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, bigquery.Client.SCOPE)
authed_http = google.auth.transport.requests.AuthorizedSession(credentials)
bigquery_client = bigquery.Client(credentials=credentials, _http=authed_http)
答案 2 :(得分:0)
当我找到原因/解决方案时,我自己回答了这个问题。
<强>原因:强>
google-cloud-python库使用 httplib2 ,在撰写本文时,httplib2对于python 2和python 3具有两个代码库。httplib2的Python 3版本不是使用 socks / proxy 支持实施。请参阅httplib2's repo#init_py。
解决方法:
有一个discussion将google-cloud-python从httplib2移动到urllib3,但同时可以使用httplib2shim
import google.auth
import httplib2shim
import google_auth_httplib2
// More declarative way exists, but left for simplicity
os.environ["HTTP_PROXY"] = "someproxy:80"
os.environ["HTTPS_PROXY"] = "someproxy:80"
http_client = httplib2shim.Http()
credentials, _ = google.auth.default()
# IMO, Following 2 lines should be done at the google-cloud-python
# This exposes client speicific logic, and it already does that
credentials = google.auth.credentials.with_scopes_if_required
(credentials, bigquery.Client.SCOPE)
authed_http = google_auth_httplib2.AuthorizedHttp(credentials,http_client)
bigquery_client = bigquery.Client(credentials=credentials, _http=authed_http)
答案 3 :(得分:0)
仅设置以下环境变量对我有用。但是我使用的是Python 2.7,安装了shadowsocks代理客户端并在1087端口上监听。
os.environ["http_proxy"] = "http://127.0.0.1:1087"
os.environ["https_proxy"] = "http://127.0.0.1:1087"