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.