使用ADF Web API活动在ADLS GEN2创建带内容文件遇缺失头错误求助
问题描述
需要在Azure Data Factory(ADF)中通过Web API活动向Azure Data Lake Storage Gen2(ADLS GEN2)创建带内容的文件。当前尝试向已有文件写入内容的请求配置如下:
{ "method": "PUT", "headers": { "x-ms-date": "Wed, 05 Mar 2025 08:25:13 GMT", "Content-Length": "1048", "x-ms-version": "2015-02-21", "x-ms-blob-condition-appendpos": "2097152 ", "x-ms-blob-condition-maxsize": "4194304" }, "url": "https://sample.dfs.core.windows.net/dsa-f/saf/temp/dsa/archive/test_aud.done?comp=appendblock", "connectVia": { "referenceName": "dsadfesf", "type": "IntegrationRuntimeReference" }, "body": "{Data_Count:2}", "authentication": { "type": "MSI", "resource": "https://storage.azure.com/" } }
执行后触发错误:
{"error":{"code":"MissingRequiredHeader","message":"An HTTP header that's mandatory for this request is not specified.\nRequestId:safsf-ewj-sjcb-315b-cdsccdsc\nTime:2025-03-05T16:35:49.3616136Z"}}
错误原因与修复
出现MissingRequiredHeader错误的核心原因:
- Endpoint错误:使用了ADLS Gen2的DFS端点(
dfs.core.windows.net),但appendblock是Blob存储兼容层的接口,必须使用Blob端点(blob.core.windows.net)。 - 头信息问题:
- 硬编码的
x-ms-date会导致请求时间过期,需用动态表达式生成合法的UTC时间。 Content-Length与实际body长度不匹配,需动态计算。x-ms-blob-condition-appendpos值后多余空格会导致校验失败。
- 硬编码的
- 接口前置条件:
appendblock仅能操作已存在的AppendBlob类型文件,如果文件未创建,需先执行创建AppendBlob的请求。
实现方案
方案1:先创建AppendBlob,再追加内容
步骤1:创建AppendBlob(Web API活动)
{ "method": "PUT", "headers": { "x-ms-date": "@utcNow('R')", "x-ms-version": "2021-08-06", "x-ms-blob-type": "AppendBlob" }, "url": "https://sample.blob.core.windows.net/dsa-f/saf/temp/dsa/archive/test_aud.done", "connectVia": { "referenceName": "dsadfesf", "type": "IntegrationRuntimeReference" }, "authentication": { "type": "MSI", "resource": "https://storage.azure.com/" } }
步骤2:追加内容到AppendBlob(Web API活动)
{ "method": "PUT", "headers": { "x-ms-date": "@utcNow('R')", "Content-Length": "@string(length(variables('bodyContent')))", "x-ms-version": "2021-08-06", "x-ms-blob-condition-appendpos": "0" }, "url": "https://sample.blob.core.windows.net/dsa-f/saf/temp/dsa/archive/test_aud.done?comp=appendblock", "connectVia": { "referenceName": "dsadfesf", "type": "IntegrationRuntimeReference" }, "body": "{\"Data_Count\":2}", "authentication": { "type": "MSI", "resource": "https://storage.azure.com/" } }
说明:
- 提前定义变量
bodyContent存储要写入的内容,确保Content-Length与实际长度匹配。 @utcNow('R')生成符合RFC1123格式的UTC时间,避免请求过期。
方案2:直接创建并写入内容(一次性写入)
如果不需要追加内容,可直接用Put Blob接口一步完成文件创建与写入:
{ "method": "PUT", "headers": { "x-ms-date": "@utcNow('R')", "Content-Length": "@string(length(variables('bodyContent')))", "x-ms-version": "2021-08-06", "x-ms-blob-type": "BlockBlob" }, "url": "https://sample.blob.core.windows.net/dsa-f/saf/temp/dsa/archive/test_aud.done", "connectVia": { "referenceName": "dsadfesf", "type": "IntegrationRuntimeReference" }, "body": "{\"Data_Count\":2}", "authentication": { "type": "MSI", "resource": "https://storage.azure.com/" } }
关键注意事项
- 系统分配托管标识需拥有目标存储容器的Storage Blob Data Contributor权限,否则会触发认证失败。
- 必须使用Blob端点(
blob.core.windows.net),DFS端点不支持Blob类型文件的REST操作。 - 所有头信息需严格符合接口规范,避免格式错误或值不匹配。
内容的提问来源于stack exchange,提问作者Sharma
相关产品推荐
相关产品推荐

