I created a Microsoft Entra Id (App registration) with name of venkat789.
Portal:
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
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
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.