Azure Translate API在Postman正常,VS Code调用返回202却无输出文件求助
Azure文档翻译API Python调用返回202但无输出文件问题
我正在测试Azure Translate REST API,尝试仅翻译单个文档而非整个容器内容。通过Postman调用时,使用对应请求头和JSON请求体可得到202状态码,且成功在目标输出容器生成翻译文档。但使用Microsoft文档提供的模板代码在VS Code中用Python执行相同调用时,虽然同样返回202 Accepted状态码,输出Blob容器中却未生成翻译文件。
调用代码
import requests endpoint = 'https://<my-endpoint>.cognitiveservices.azure.com/' key = '<my-key>' path = 'translator/text/batch/v1.1/batches' constructed_url = endpoint + path sourceSASUrl = "https://<my-blob>.blob.core.windows.net/input/christmasemailcopy.docx?sp=rwl&st=2023-12-11T11:39:20Z&se=2023-12-11T19:39:20Z&sv=2022-11-02&sr=c&sig=<sas-token-here>" targetSASUrl = "https://<my-blob>.blob.core.windows.net/output/christmasemailcopyes3.docx?sp=rcwl&st=2023-12-11T11:40:52Z&se=2023-12-11T19:40:52Z&sv=2022-11-02&sr=c&sig=<sas-token-here>" body= { "inputs": [ { "source": { "sourceUrl": sourceSASUrl, "language": "en" }, "targets": [ { "targetUrl": targetSASUrl, "language": "es" } ] } ] } headers = { 'Ocp-Apim-Subscription-Key': key, 'Content-Type': 'application/json', 'ocp-Apim-subscription-reigon' : 'northeurope' } response = requests.post(constructed_url, headers=headers, json=body) response_headers = response.headers print(f'response status code: {response.status_code}\nresponse status: {response.reason}\n\nresponse headers:\n') for key, value in response_headers.items(): print(key, ":", value)
核心问题排查与解决方案
请求头拼写错误
代码中ocp-Apim-subscription-reigon存在拼写错误,正确键名应为Ocp-Apim-Subscription-Region(注意是Region而非reigon,大小写建议与官方规范保持一致)。Postman中你大概率使用了正确的键名,而Python代码的拼写错误会导致服务无法正确识别资源区域——虽然返回202表示请求已接受,但实际翻译任务并未被正常调度执行。任务状态验证
202状态码仅代表请求被接收,不代表任务成功完成。你需要通过响应头中的Operation-LocationURL查询任务实际状态,获取执行日志或错误信息,这是排查此类问题的关键步骤。
修正后的代码示例
import requests endpoint = 'https://<my-endpoint>.cognitiveservices.azure.com/' key = '<my-key>' path = 'translator/text/batch/v1.1/batches' constructed_url = endpoint + path sourceSASUrl = "https://<my-blob>.blob.core.windows.net/input/christmasemailcopy.docx?sp=rwl&st=2023-12-11T11:39:20Z&se=2023-12-11T19:39:20Z&sv=2022-11-02&sr=c&sig=<sas-token-here>" targetSASUrl = "https://<my-blob>.blob.core.windows.net/output/christmasemailcopyes3.docx?sp=rcwl&st=2023-12-11T11:40:52Z&se=2023-12-11T19:40:52Z&sv=2022-11-02&sr=c&sig=<sas-token-here>" body= { "inputs": [ { "source": { "sourceUrl": sourceSASUrl, "language": "en" }, "targets": [ { "targetUrl": targetSASUrl, "language": "es" } ] } ] } # 修正请求头拼写错误,统一大小写规范 headers = { 'Ocp-Apim-Subscription-Key': key, 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Region': 'northeurope' } response = requests.post(constructed_url, headers=headers, json=body) response_headers = response.headers print(f'response status code: {response.status_code}\nresponse status: {response.reason}\n\nresponse headers:\n') for key, value in response_headers.items(): print(key, ":", value) # 查询任务状态(若响应头包含Operation-Location) if 'Operation-Location' in response_headers: operation_url = response_headers['Operation-Location'] status_response = requests.get(operation_url, headers={'Ocp-Apim-Subscription-Key': key}) print("\n任务状态详情:") print(status_response.json())
额外检查项
- 确认目标SAS URL权限包含**写入(w)和创建(c)**权限,确保服务能在输出容器生成文件。
- 检查目标容器是否存在,以及文件名是否包含特殊字符导致无法生成。
内容的提问来源于stack exchange,提问作者howlieT
相关产品推荐
相关产品推荐

