Azure Linux函数调用Azure AI Speech遇WS_OPEN_ERROR_UNDERLYING_IO_OPEN_FAILED错误
Azure函数部署后调用Azure AI Speech文本转语音失败问题
本地Windows开发的Python Azure函数(Linux运行环境),调用Azure AI Speech(原Azure认知服务)的文本转语音(TTS)功能时本地运行正常,但部署到Azure云后出现连接错误:
Connection failed (no connection to the remote host). Internal error: 1. Error details: Failed with error: WS_OPEN_ERROR_UNDERLYING_IO_OPEN_FAILED wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1 X-ConnectionId: 45e9f87a28fa40ba981221cb55f6fc15 USP state: Sending. Received audio size: 0 bytes.
代码实现
try: file_url = None speech_config = speechsdk.SpeechConfig( subscription=my_azure_ai_speech_api_key, region="eastus" ) local_audio_path = f"/tmp/{filename}" ensure_directory_exists(local_audio_path) audio_config = speechsdk.audio.AudioOutputConfig(filename=local_audio_path) synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) result = synthesizer.speak_text_async(text).get() if result.reason == speechsdk.ResultReason.Canceled: cancellation_details = result.cancellation_details cancellation_reason = cancellation_details.reason cancellation_error = "" if cancellation_details.reason == speechsdk.CancellationReason.Error: cancellation_error = cancellation_details.error_details # 日志记录错误逻辑,上述报错就是在这里触发的 return with open(local_audio_path, "rb") as audio_file: audio_data = audio_file.read() # 将音频写入Azure Blob存储的逻辑 # 删除本地文件的逻辑 return file_url except Exception as e: # 日志记录错误逻辑
已尝试的解决方案
安装OpenSSL 1.1的启动脚本
创建startup.sh脚本用于环境初始化:
#!/bin/bash # 记录脚本启动 echo "Starting startup script" # 更新包列表 echo "Updating package lists" apt-get update # 安装依赖 echo "Installing dependencies" apt-get install -y build-essential libssl-dev ca-certificates libasound2 wget # 安装OpenSSL 1.1 echo "Installing OpenSSL 1.1" wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb dpkg -i libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb # 验证安装 echo "Verifying OpenSSL installation" openssl version # 记录脚本结束 echo "Startup script completed"
配置环境变量
为函数应用添加以下环境变量:
WEBSITE_STARTUP_SCRIPT=startup.sh WEBSITE_RUN_FROM_PACKAGE=1
其他排查操作
- 确认Azure AI Speech的API密钥和区域配置正确(已在函数应用中留存记录)
- 在Azure AI Speech服务的访问控制(IAM)中,将函数应用添加为Azure AI Developer角色
- 多次重新部署并重启函数应用
以上操作均未解决问题。
内容的提问来源于stack exchange,提问作者Robin
相关产品推荐
相关产品推荐

