使用Microsoft Entra ID(服务主体)授权上传文件至ADLS Gen2失败
问题描述
尝试通过Microsoft Entra ID(服务主体)授权上传文件至ADLS Gen2存储,使用DataLakeServiceClient时触发400错误,提示缺少必填请求头x-ms-type。
错误详情:
com.azure.storage.file.datalake.models.DataLakeStorageException: Status code 400, "?
<Code>MissingRequiredHeader</Code><Message>An HTTP header that's mandatory for this request is not specified. RequestId:adad-dads-d-20a2-xxdadad Time:2024-05-07T17:18:58.0750289Z</Message><HeaderName>x-ms-type</HeaderName></Error>
使用的Java代码:
DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder() .endpoint("https://<storageaccount>.file.core.windows.net/") .credential(new ClientSecretCredentialBuilder() .clientId("<client_id>") .clientSecret("<client_secret>") .tenantId("<tenantId>") .build()).buildClient(); DataLakeFileSystemClient container = dataLakeServiceClient.getFileSystemClient("test"); DataLakeDirectoryClient directoryClient = container.getDirectoryClient("upload").getSubdirectoryClient("customer_info"); InputStream stream = new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)); long fileLength; try { fileLength = stream.available(); DataLakeFileClient fileClient = directoryClient.createFileIfNotExists("test.csv"); //fileClient.cr fileClient.upload(stream, fileLength); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
POM依赖配置:
<dependency> <groupId>com.azure</groupId> <artifactId>azure-storage-file-datalake</artifactId> <version>12.18.3</version> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId> <version>5.11.0</version> </dependency>
解决方案
错误根源是你误用了Azure File Storage的endpoint(*.file.core.windows.net)访问ADLS Gen2资源,ADLS Gen2必须使用专属的数据湖存储endpoint(*.dfs.core.windows.net)。
修改步骤:
- 将
DataLakeServiceClientBuilder中的endpoint替换为https://<storageaccount>.dfs.core.windows.net/ - 重新执行代码即可解决问题。
补充说明:ADLS Gen2构建在Blob存储之上,只有通过dfs endpoint才能触发数据湖的专属功能与头部校验逻辑,file endpoint仅适用于Azure File Storage服务,二者不可混用。
内容的提问来源于stack exchange,提问作者Ratheesh
相关产品推荐
相关产品推荐

