skip to Main Content

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


  1. Looking at the REST API documentation here, I am guessing that the missing header is x-ms-file-request-intent which is required if you are authorizing your request using OAuth token.

    enter image description here

    Please make sure that your Service Principal has appropriate permissions (as mentioned in the document) and add x-ms-file-request-intent with value as backup in your request.

    Login or Signup to reply.
  2. Try using the share service client options to configure the http client, to add the missing header.

    type ShareClientOptions = StoragePipelineOptions & ShareClientConfig
    

    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.

    Solved similar experience in C#, here after

    The configuration below worked with an HttpTriggered Azure function using DefaultAzureCredential in local:

    • I gave file roles to my account through IAM access control in Azure Portal, Storage File Data Privileged Reader and Storage File Data Privileged Contributor, on the file share.
    • Use the option ShareTokenIntent.Backup as value of the ShareTokenIntent field when creating the ShareClientOptions instance.
    
    private ShareClient GetAzureShareClient(DefaultAzureCredential defaultAzureCredential)
    {
       return new ShareClient(new Uri("https://myaccount.file.core.windows.net/myfileshare"), defaultAzureCredential, new ShareClientOptions() { ShareTokenIntent = ShareTokenIntent.Backup });
    }
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search