I’m trying to access an Azure File Share using the Azure SDK for JavaScript/TypeScript. My goal is to fetch the properties of a specific file. However, I’m encountering a MissingRequiredHeader error when attempting to get the file properties.
const { ShareServiceClient } = require("@azure/storage-file-share");
const { ClientSecretCredential } = require("@azure/identity");
// Azure credentials
const tenantId = "";
const clientId = "";
const clientSecret = "";
// Storage configuration
const storageAccountName = '';
const fileShareName = 'documents';
// File path
const directoryPath = "uploadcontent/xyz/2024/MAR/abc";
const fileName = "abc.pdf";
// Create a ClientSecretCredential
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Create ShareServiceClient
const shareServiceClient = new ShareServiceClient(
`https://${storageAccountName}.file.core.windows.net`,
credential
);
async function getFileProperties() {
try {
const shareClient = shareServiceClient.getShareClient(fileShareName);
const directoryClient = shareClient.getDirectoryClient(directoryPath);
const fileClient = directoryClient.getFileClient(fileName);
const properties = await fileClient.getProperties();
console.log("File properties:", properties);
} catch (error) {
console.error("An error occurred:", error.message);
if (error.details) {
console.error("Error details:", error.details);
}
}
}
getFileProperties().catch(console.error);
Error Message
When running this code, I get the following error:
An error occurred:
Error details: {
errorCode: 'MissingRequiredHeader',
'transfer-encoding': 'chunked',
server: 'Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0',
'x-ms-request-id': '730b5b9b-901a-0046-7257-1ac07c000000',
'x-ms-client-request-id': '2b148100-1625-45e3-8215-a8a3edc25e99',
'x-ms-version': '2024-11-04',
date: 'Wed, 09 Oct 2024 14:28:06 GMT',
body: undefined
}
What I’ve Tried
I’ve verified that the service principal (clientId) has the necessary permissions to access the file share.
I’ve double-checked that the file path and share name are correct.
I’ve tried adding custom headers to the request, including ‘x-ms-version’ and ‘x-ms-date’, but the issue persists.
Question
What could be causing this MissingRequiredHeader error?
Are there any specific headers or configurations I need to include when using ClientSecretCredential with Azure File Share?
Is there a better way to authenticate and access Azure File Share using the JavaScript/TypeScript SDK?
Any help or guidance would be greatly appreciated. Thank you!
2
Answers
Looking at the REST API documentation
here
, I am guessing that the missing header isx-ms-file-request-intent
which is required if you are authorizing your request using OAuth token.Please make sure that your Service Principal has appropriate permissions (as mentioned in the document) and add
x-ms-file-request-intent
with value asbackup
in your request.Try using the share service client options to configure the http client, to add the missing header.
Storage pipeline options infos
The cause of the missing header: As Mantri said before, x-ms-file-intent is required for OAuth token operations with Azure Files REST API.
And was not automatically added when creating your shareServiceClient.
The configuration below worked with an HttpTriggered Azure function using DefaultAzureCredential in local:
I got the hint from here: authorized operations against the Azure Files service
This documentation about JS library for Azure Files might help Azure Storage File Data Lake client library for JavaScript