How to use DefaultAzureCredential in Azure ShareServiceClient

How to use DefaultAzureCredential in Azure ShareServiceClient
typescript
Ethan Jackson

Following the Azure file share storage example for shared credentials we were able to successfully connect and upload files.

When required to switch to DefaultAzureCredential we changed the ShareClient code as follows:

const serviceClient = new ShareServiceClient( fileShareUrl, new DefaultAzureCredential() ); const shareClient = serviceClient.getShareClient(shareName); const directoryClient = shareClient.rootDirectoryClient; const fileClient = directoryClient.getFileClient(fileName); await fileClient.create(bytes);

However this causes the fileclient.create() receives a required header error:

code: 'MissingRequiredHeader', statusCode: 400, details: { errorCode: 'MissingRequiredHeader', 'content-length': '305', 'content-type': 'application/xml', server: 'Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0', 'x-ms-request-id': 'b8633a98-501a-007c-3aca-c5dba1000000', 'x-ms-client-request-id': '723a7dec-cb34-48ee-95e5-281bbbee0714', 'x-ms-version': '2025-05-05', date: 'Thu, 15 May 2025 18:54:43 GMT', message: "An HTTP header that's mandatory for this request is not specified.\n" + 'RequestId:b8633a98-501a-007c-3aca-c5dba1000000\n' + 'Time:2025-05-15T18:54:43.1738490Z', authenticationErrorDetail: undefined, code: 'MissingRequiredHeader', HeaderName: 'x-ms-file-request-intent' }

I have tried the following ideas found in the documentation:

1.

npm install @azure/storage-file-share@latest
await fileClient.create(1024, { customHeaders: { 'x-ms-file-request-intent': 'transactional', }, });
await fileClient.create(1024, { customHeaders: { 'x-ms-file-request-intent': ‘backup’, }, });
await fileClient.setHttpHeaders({ 'x-ms-file-request-intent': 'transactional', });

Is there documentation on how the Javascript SDK sets this header?

Answer

After speaking with Azure tech support, i was provided the following solution, and it does allow the header to be included by the SDK and the file uploaded successfully.

const serviceClient = new ShareServiceClient( fileShareUrl, new DefaultAzureCredential(), { fileRequestIntent: 'backup' } );

Please note that DefaultAzureCredential is using the following env vars:

process.env.AZURE_CLIENT_ID process.env.AZURE_TENANT_ID process.env.AZURE_CLIENT_SECRET; process.env.AZURE_CLIENT_URL

Related Articles