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

Lavate, Ramchandra (Cognizant) 0 Reputation points
2024-05-06T06:18:06.8766667+00:00

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();

System.out.println(">>>>>>>>>>>11"+file.getProperties().getLastModified());

}else if(item instanceof CloudFileDirectory){

System.out.println(">>>>>>>>>>>2"+item);

}

}

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
    2024-05-06T11:37:14.01+00:00

    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:

    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-file-share</artifactId>
        <version>12.14.0</version>
    </dependency>
    

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

    import com.azure.storage.file.share.ShareServiceClient;
    import com.azure.storage.file.share.ShareServiceClientBuilder;
    public class AzureStorageService {
        private final String connectionString = "your-connection-string";
        public ShareServiceClient getShareServiceClient() {
            return new ShareServiceClientBuilder()
                    .connectionString(connectionString)
                    .buildClient();
        }
    }
    

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

    import com.azure.storage.file.share.ShareFileClient;
    import com.azure.storage.file.share.ShareDirectoryClient;
    import com.azure.storage.file.share.models.ShareFileItem;
    import com.azure.storage.file.share.models.ShareFileProperties;
    
    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)
                    .getDirectoryClient(directoryPath);
    
            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 - https://learn.microsoft.com/en-us/answers/questions/1604096/how-to-return-file-name-and-last-modified-from-fil

    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