Access VM fileshare from Azure function app

Arulanand Anandhakumar 20 Reputation points
2024-07-12T08:26:52.5766667+00:00

Title: Accessing FileShare from Azure Function App in Same VNet

Hi,

I am trying to access files in a FileShare from an Azure Function App. The FileShare is hosted inside an Azure VM, and both the VM and the Function App are within the same VNet. However, they are not in the same App Service Plan. I am using a .NET 8 Function App.

I have attempted two different code approaches, but my network engineer reports no traffic from the Azure Function. Additionally, I encounter different errors with each approach.

Could someone provide guidance on resolving this issue?

Approach 1:


string fullPath = Path.Combine(FileSharePath, data.FilePath);

var networkCredential = new NetworkCredential("username", "password", "domain");

using (new NetworkConnection(FileSharePath, networkCredential))

{

    if (File.Exists(fullPath))

    {

        using (var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read))

        {

            // Do something with the fileStream

        }

        return new OkResult();

    }

    return new NotFoundObjectResult("File not found");

}

Error: Network path was not found

Approach 2:


await using var fileStream = new FileStream(data.FilePath, FileMode.Open, FileAccess.Read);

return new OkResult();

Error: Access denied to path

Any help to tackle this challenge would be greatly appreciated.

Thank you!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,117 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,001 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. TP 98,176 Reputation points
    2024-07-12T12:50:37.82+00:00

    Hi,

    The sandbox blocks the ports required for connecting to SMB share:

    https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#restricted-outgoing-ports

    Please click Accept Answer and upvote if the above was helpful.

    Thanks.

    -TP


  2. Udayashankar K.N 80 Reputation points Microsoft Employee
    2024-07-15T04:53:26.7933333+00:00

    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


  3. Udayashankar K.N 80 Reputation points Microsoft Employee
    2024-07-15T13:49:32.8233333+00:00

    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).

    however the alternate is to use the Storage Blob using the URL.


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.