简体   繁体   English

无法使用来自 python 的代理下载 azure blob

[英]Can´t download azure blob using proxies from python

I´m trying to download a azure blob from a server which uses proxy.我正在尝试从使用代理的服务器下载 azure blob。 Right now, my code works perfectly in my local (without using proxy as it is not needed) but when I try to run it from a server where I need to use the proxy, it doesn´t work even though I followed the azure documentation.现在,我的代码在我的本地完美运行(不需要使用代理,因为它不需要)但是当我尝试从我需要使用代理的服务器运行它时,即使我遵循了 azure 文档,它也不起作用. I get the following error:我收到以下错误:

azure.core.exceptions.ClientAuthenticationError: Authentication failed: <urllib3.connection.HTTPSConnection object at 0x7fa5b831f760>: Failed to establish a new connection: [Errno -2] Name or service not known

This is the code:这是代码:

from azure.identity import ClientSecretCredential
from azure.storage.blob import BlobServiceClient 
credential = ClientSecretCredential(self.tenant_id, self.client_id, self.client_secret)
proxy = {"http": proxy_value, "https": proxy_value}
blob_service_client_instance = BlobServiceClient(
    account_url=storage_account_url, credential=credential, proxies=proxy, connection_verify=False
)
blob_client_instance = blob_service_client_instance.get_blob_client(
    container_name, blob_name, snapshot=None
)
blob_data = blob_client_instance.download_blob()

I´m 100% sure that all the values are correct as I have done other different tests that work.我 100% 确定所有值都是正确的,因为我已经完成了其他有效的不同测试。

Thanks in Advance!!提前致谢!!

I tried in my environment and got below results:我在我的环境中尝试并得到以下结果:

Initially I tried in my environment and got error,最初我在我的环境中尝试并出现错误,

(Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x04755510>: Failed to establish a new connection: [Errno11001] getaddrinfo failed',))) (由 ProxyError('无法连接到代理。', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x04755510>: Failed to establish a new connection: [Errno11001] getaddrinfo failed',)))

The error occurs due to your proxy password may contains an @ symbol , which is causing your URL parsing to get confused.发生错误是因为您的代理密码可能包含一个@ symbol ,这导致您的 URL 解析变得混乱。 You need to URL encode your password (that is, run it through urllib.quote) before passing it or it gets pretty confusing.在传递密码之前,您需要对密码进行URL encode (即,通过urllib.quote) ,否则它会变得非常混乱。

You can refer this document to set variable in the azure cli.您可以参考此文档在 azure cli 中设置变量。 I tried with urllib.parse with below python code I downloaded and read the content of file with proxy successfully.我尝试使用带有以下urllib.parse代码的 urllib.parse 下载并使用代理成功读取文件内容。

from azure.identity import ClientSecretCredential
from azure.storage.blob import BlobServiceClient
import json
import os
import requests
from urllib.parse import quote


username = quote('type in username')  
password = quote(' type in password')  
proxy = 'type in proxy name or ip'  
proxy_port = '8080'

proxy_https = 'https://' + username + ':' + password + '@' + proxy + ':' + proxy_port

proxies={ 'https': proxy_https}
tenant_id="<tenant id>"
client_id="<client id>"
client_secret="<client secret>"

credential = ClientSecretCredential(tenant_id,client_id,client_secret)
storage_url="https://venkat12.blob.core.windows.net"

container_name="test"
blob_name="file.json"
blob_service_client_instance = BlobServiceClient(account_url=storage_url,proxy=proxies,credential=credential,connection_verify=True)
blob_client_instance = blob_service_client_instance.get_blob_client(
    container_name,blob_name, snapshot=None
)
blob_data = blob_client_instance.download_blob().readall()
raw=json.loads(blob_data)
print(raw)

Console:安慰:在此处输入图像描述

Reference: Can't get Proxy to work with https and http works only with auth, not like the Docu descripes · Issue #3990 · psf/requests (github.com) by eisbaer9.参考:无法让代理与 https 一起工作,而 http 仅与 auth 一起工作,不像 Docu 描述的 · Issue #3990 · psf/requests (github.com) by eisbaer9。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM