Azure Databricks向SharePoint传输文件时出现SSL错误求助
问题背景
在Azure Databricks Python笔记本中使用Office365-REST-Python-Client包向SharePoint站点传输文件时,遇到SSL连接错误:
SSLError: HTTPSConnectionPool(host='<SP_domain>.sharepoint.com', port=443): Max retries exceeded with url: /sites/<my sites name>(Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))
最终触发AttributeError: 'NoneType' object has no attribute 'text',根源为上述SSL错误。本地环境测试正常,已通过SharePoint的appregnew.aspx获取合法的client_id和client_secret,且尝试过三种禁用SSL验证的方案均无效:
- 直接设置RequestOptions的verify属性,触发
TypeError: sequence item 2: expected str instance, RequestOptions found - 猴子补丁requests库,仍出现重试超限错误
- 通过pending_request的beforeExecute事件设置verify=False,问题未解决
可行解决方案
1. 直接修改ClientContext底层Session的SSL验证配置
Office365库的ClientContext底层基于requests Session,可直接修改其verify属性,跳过SSL验证(或指定CA证书路径):
from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.client_credential import ClientCredential site_url = "https://<SP_domain>.sharepoint.com/sites/<my_site_name>" client_id = "你的client_id" client_secret = "你的client_secret" # 初始化上下文并配置凭证 ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret)) # 禁用SSL验证(或替换为Databricks信任的CA证书路径) ctx.pending_request().context.session.verify = False # 测试连接 web = ctx.web.get().execute_query() print(f"站点标题: {web.properties['Title']}")
2. 排查Databricks网络出站连通性
SSL错误可能源于Databricks集群无法正常访问SharePoint域名,先验证基础网络连通性:
import requests try: resp = requests.get("https://<SP_domain>.sharepoint.com", verify=False, timeout=10) print(f"HTTP状态码: {resp.status_code}") except Exception as e: print(f"网络连接失败: {str(e)}")
若测试失败,需联系管理员调整Databricks集群的出站网络规则,确保允许访问SharePoint的443端口。
3. 升级Office365-REST-Python-Client版本
旧版本库可能存在SSL握手相关的bug,升级至最新版本:
%pip install --upgrade office365-rest-python-client
4. 强制指定TLS版本
部分情况下,Databricks默认TLS版本与SharePoint不兼容,可强制使用TLS 1.2:
import ssl import requests from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.client_credential import ClientCredential # 创建强制TLS 1.2的SSL上下文 ssl_ctx = ssl.create_default_context() ssl_ctx.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 # 配置自定义Session custom_session = requests.Session() custom_session.verify = False custom_session.mount("https://", requests.adapters.HTTPAdapter(ssl_context=ssl_ctx, max_retries=2)) # 绑定自定义Session到ClientContext ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret)) ctx.pending_request().context.session = custom_session # 执行文件上传测试 target_folder = ctx.web.get_folder_by_server_relative_url("Shared Documents") with open("/dbfs/path/to/your/file", "rb") as f: target_folder.upload_file("uploaded_file_name.ext", f).execute_query()
内容的提问来源于stack exchange,提问作者Siete
相关产品推荐
相关产品推荐

