Authenticate Azure Storage Files using Microsoft Entra ID

Saurabh Bhandari 0 Reputation points
2025-04-18T11:32:16.5666667+00:00

Hi team,

I have a storage account with all permissions needed for Microsoft Entra ID, that is Storage File Data Privileged Reader and Storage File Data Privileged Contributor.

  • I am able to create, get, delete files, and listfileshare using SAS or Storage Key.
  • I am able to get only a list of file shares using Microsoft Entra ID
  • I am not able to create, get, or delete files using Microsoft Entra ID

I am using the below SDK and Java 8:

``


Code Sample:

 TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
          .clientId("id") 
          .clientSecret("secret") 
          .tenantId("tenantid")  
          .build();



ShareServiceClient serviceClient = new ShareServiceClientBuilder()
          .endpoint(fileShareUrl)
          .credential(tokenCredential)
          .shareTokenIntent(ShareTokenIntent.BACKUP)
          .buildClient();

 ShareClient shareClient = serviceClient.getShareClient("files");

      String fileName = "testfile.txt";
      ShareFileClient fileClient = shareClient.getFileClient(fileName);

      byte[] fileContent = "Hello, Azure File Share!".getBytes(StandardCharsets.UTF_8);
      long fileSize = fileContent.length;

       fileClient = shareClient.createFile(fileName,
         fileSize);
      fileClient.upload(new ByteArrayInputStream(Base64.decodeBase64(fileContent)),
          Base64.decodeBase64(fileContent).length, new ParallelTransferOptions());

      System.out.println("File uploaded successfully!");

Ask:

  1. Is there any other permission needed for it?
  2. Is there something I am doing wrong?
  3. Also, I want to know to authenticate through Microsoft Entra ID. Do I need to configure identity-based access for this?
    User's image
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,401 questions
{count} votes

Accepted answer
  1. Venkatesan S 1,475 Reputation points Microsoft External Staff
    2025-04-24T10:01:07.3766667+00:00

    Hi Saurabh Bhandari

    I created a Microsoft Entra Id (App registration) with name of venkat789.

    Portal:

    enter image description here

    I have now assigned the Storage File Data Privileged Reader and Storage File Data Privileged Contributor RBAC roles to the Microsoft Entra ID at the storage account level.

    Once you have assigned it, you can verify through the portal or using an Azure CLI command. In my environment, I verified it using an Azure CLI command.

    Command and output:

    xxxxxxxx [ ~ ]$ az role assignment list --assignee "e25b10xxxxxxxx9f720c55bace" --all --output table
    Principal                             Role                                      Scope
    ------------------------------------  ----------------------------------------  -----------------------------------------------------------------------------------------------------------------------------------------
    e25b1025-3fxxxxxxxxxxx3-9f720c55bace  Storage File Data Privileged Contributor  /subscriptions/xxxxx/resourceGroups/venkatesan-rg/providers/Microsoft.Storage/storageAccounts/venkat326123
    e25b1025-3fxxxxxxxxxxx3-9f720c55bace  Storage File Data Privileged Reader       /subscriptions/xxxxxx/resourceGroups/venkatesan-rg/providers/Microsoft.Storage/storageAccounts/venkat326123
    

    enter image description here

    Now, I used the below code, to

    • list files from share
    • Create and upload a file
    • Download/get the file content
    • Delete the file

    with Microsoft Entra ID authentication using Azure Java SDK.

    Code:

    public class App {
    
        public static void main(String[] args) {
            // Entra ID credentials and storage details
            String tenantId = "9xxxxx";
            String clientId = "xxxxx";
            String clientSecret = "xxxx";
            String storageAccountName = "xxxxxx";
            String fileShareUrl = String.format("https://%s.file.core.windows.net", storageAccountName);
            String shareName = "xxxx";
            String fileName = "testfile.txt";
    
            // Authenticate using Microsoft Entra ID
            ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                    .tenantId(tenantId)
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .build();
    
            ShareServiceClient serviceClient = new ShareServiceClientBuilder()
                    .endpoint(fileShareUrl)
                    .credential(credential)
                    .shareTokenIntent(ShareTokenIntent.BACKUP)
                    .buildClient();
    
            // Create and upload a file
            ShareClient shareClient = serviceClient.getShareClient(shareName);
            ShareFileClient fileClient = shareClient.getRootDirectoryClient().getFileClient(fileName);
            byte[] content = "Hello from Microsoft Entra ID!".getBytes(StandardCharsets.UTF_8);
            fileClient.create(content.length);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
            ParallelTransferOptions options = new ParallelTransferOptions()
                   .setBlockSizeLong(4 * 1024 * 1024L)
                   .setMaxConcurrency(2);
            fileClient.upload(inputStream, content.length, options);
            System.out.println("File created and uploaded: " + fileName);
    
            // Download file
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            fileClient.download(stream);
            System.out.printf("Completed downloading the file with content: %n%s%n%n",
                    new String(stream.toByteArray(), StandardCharsets.UTF_8));
    
            // Recursive listing
            System.out.println("Recursively listing all files and folders:");
            listFilesRecursively(shareClient.getRootDirectoryClient(), "");
    
            // Delete file
            fileClient.delete();
            System.out.println("File deleted: " + fileName);
        }
    
        // Recursive listing helper
        public static void listFilesRecursively(ShareDirectoryClient directoryClient, String path) {
            for (ShareFileItem item : directoryClient.listFilesAndDirectories()) {
                String fullPath = path.isEmpty() ? item.getName() : path + "/" + item.getName();
                if (item.isDirectory()) {
                    System.out.println("Directory: " + fullPath);
                    ShareDirectoryClient subDirClient = directoryClient.getSubdirectoryClient(item.getName());
                    listFilesRecursively(subDirClient, fullPath);
                } else {
                    System.out.println("File: " + fullPath);
                }
            }
        }
    }
    

    Output:

    File created and uploaded: testfile.txt
    Completed downloading the file with content: 
    Hello from Microsoft Entra ID!
    
    Recursively listing all files and folders:
    File: demo.jpg
    Directory: sample
    File: sample/aml.gif
    File: sample/aml1.gif
    File: sample/aml2.gif
    File: sample/copyblob.gif
    File: sample/copyblob1.gif
    File: sample/copyblob2.gif
    File: sample.jpg
    File: testfile.txt
    File: xxxxxxx.zip
    File deleted: testfile.txt
    

    enter image description here

    Reference: Enable access to Azure file shares using OAuth over REST | Microsoft Learn

    Hope this answer helps! please let us know if you have any further queries. I’m happy to assist you further.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.