Accessing Azure SMB File Shares From Function App

William Beglen 25 Reputation points
2023-04-24T12:15:44.1+00:00

I have an Azure function app that needs to move files around between Azure file shares. My function app contains a lot of code that uses System.IO (C#, .Net) file system classes. My issue is that I'm new to Azure, and I'm not sure how to access an Azure SMB share from within a function app using System.IO methods. Even the file enumeration methods return an 'access denied' exception. Since Azure file shares support SMB protocol, is there any reason why existing .Net System.IO classes can't be used inside an Azure environment by a function app rather than rewrite all of our existing file handline code to use the REST API interface client? Do I need to mount a file share inside of azure to use System.IO?

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,318 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,185 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Konstantinos Passadis 19,266 Reputation points MVP
    2023-04-24T12:24:33.7833333+00:00

    Hello @William Beglen Yes, you can use System.IO classes to access files in an Azure file share from within an Azure function app. However, you will need to configure the function app to authenticate to the file share and mount it as a drive letter or UNC path. Here are the steps to do this: Create a storage account and file share in Azure if you haven't already done so. Open the Function App settings in the Azure portal and go to the "Configuration" section.

    Add a new application setting with the name "STORAGE_CONNECTION_STRING" and the value set to the connection string for your storage account. You can find the connection string in the "Access keys" section of the storage account. In your function app code, use the following code to authenticate to the file share and mount it as a drive letter or UNC path:

    string storageConnectionString = Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING"); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare fileShare = fileClient.GetShareReference("yourfilesharename"); fileShare.CreateIfNotExists(); CloudFileDirectory rootDir = fileShare.GetRootDirectoryReference(); rootDir.CreateIfNotExists(); string mappedDrive = Path.GetTempPath() + "yourfilesharename"; rootDir.Mount(mappedDrive); This code will mount the file share at a temporary directory on the function app, which can be accessed using standard System.IO classes. Note that if your file share is in a different Azure region than your function app, you may experience slower performance and higher latency due to network traffic. In this case, you may want to consider using the REST API interface client or a dedicated file transfer solution such as Azure File Sync or Azure Data Box.

    Please mark the answer as Accepted in case it helped ! BR


  2. MuthuKumaranMurugaachari-MSFT 22,361 Reputation points
    2023-04-28T14:58:37.5966667+00:00

    William Beglen Thanks for posting your question in Microsoft Q&A. Yes, you need to mount file share before accessing it. Currently, Mount file shares are only supported for Linux Web Apps (and Windows Container Web apps), and you can follow doc: Mount file shares in setting the mount via CLI az webapp config storage-account add(or PowerShell command).

    Then you can use System.IO to access files with the mount-path and I found article https://www.serverlessnotes.com/docs/mounting-file-shares-on-linux-azure-function-apps created by MVP that would help with your scenario. Also, .NET SDK can be used to access file shares as described in Access the file share programmatically (after setting up Configure Azure Storage connection strings). I hope this helps with your question and let me know if you have any questions or face issues.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


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.