How to return file name and last modified from fileshare on azure storage using java spring boot

Lavate, Ramchandra (Cognizant) 0 Reputation points

How to return file name and last modified from files

hare on azure storage using java spring boot

// Retrieve the storage account from the connection string.

CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectString);

// Create a CloudFileClient object for the storage account.

CloudFileClient fileClient = storageAccount.createCloudFileClient();

// Get a reference to the file share.

CloudFileShare share = fileClient.getShareReference(shareName);

// Get a reference to the root directory of the share.

CloudFileDirectory fileDirectory = share.getRootDirectoryReference();

// Get a reference to the target directory within the share.

CloudFileDirectory targetDir = fileDirectory.getDirectoryReference(rootDir);

// Download files to local storage.

for (ListFileItem item : targetDir.listFilesAndDirectories()) {

if (item instanceof CloudFile) {

CloudFile file = (CloudFile) item;

FileProperties properties = file.getProperties();


}else if(item instanceof CloudFileDirectory){




Its not working its return as null

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,178 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
898 questions
Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
111 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nehruji R 2,741 Reputation points Microsoft Vendor

    Hello Lavate, Ramchandra (Cognizant),

    Greetings! Welcome to Microsoft Q&A Platform.

    To retrieve the filename and last modified timestamps from files in an Azure file share using Java Spring Boot, you can try following these steps to get the expected output,

    Confirm that your application has the necessary permissions to read files from the specified share. Check the access policies and permissions for the storage account.

    Make sure you have the necessary dependencies in your Spring Boot project. You’ll need the Azure Storage SDK for Java. Add the following dependency to your pom.xml:


    Create a method to initialize your Azure Storage account using the connection string:

    public class AzureStorageService {
        private final String connectionString = "your-connection-string";
        public ShareServiceClient getShareServiceClient() {
            return new ShareServiceClientBuilder()

    Now, you can retrieve the file properties (including the last modified timestamp) from the file share:

    public class FileShareService {
        private final ShareServiceClient shareServiceClient;
        public FileShareService(ShareServiceClient shareServiceClient) {
            this.shareServiceClient = shareServiceClient;
        public void listFilesAndDirectories(String shareName, String directoryPath) {
            ShareDirectoryClient directoryClient = shareServiceClient.getShareClient(shareName)
            for (ShareFileItem item : directoryClient.listFilesAndDirectories()) {
                if (item.isShareFile()) {
                    ShareFileClient fileClient = directoryClient.getFileClient(item.getName());
                    ShareFileProperties properties = fileClient.getProperties();
                    System.out.println("File Name: " + item.getName());
                    System.out.println("Last Modified: " + properties.getLastModified());
                } else {
                    // Handle directories if needed

    Finally, use the listFilesAndDirectories method to retrieve the file names and last modified timestamps:

    public static void main(String[] args) {
        AzureStorageService storageService = new AzureStorageService();
        ShareServiceClient shareServiceClient = storageService.getShareServiceClient();
        FileShareService fileShareService = new FileShareService(shareServiceClient);
        fileShareService.listFilesAndDirectories("your-share-name", "your-directory-path");

    Make sure to replace the file share name, Directory name & your connection string per your requirements and verify other parameters are correctly configured.

    Note: This is a sample code, and you can adapt the code to suit your specific requirements.

    Similar thread for reference -

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

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

    0 comments No comments