Access files in VM using Azure function app

Nuwan Wickramanayaka 81 Reputation points
2023-04-11T14:29:15.46+00:00

Hi In my organization they have a Azure disk and mounted to Azure VM and use as file share. File can be accessed as network file \trmfs1.termserv.xxxx.com\termserv_apps_cust\xxxx.pdf. My requirement is, I want to access same file using RESTAPI deployed on app service. (same azure env). Networking page of the both app service and VM given below Networking page of app service (where my REST API deployed) User's image

Networking page of the Azure VM User's image

Kindly share you opinion on this. Still I cannot figure out a way to access files in VM. .NET core I have used to access the file

  WebClient request = new WebClient();
  request.Credentials = new NetworkCredential(@"nuwanw", "password");

  FileInfo myFile = new FileInfo(@"\\trmfs1.termserv.xxxx.com\termserv_apps_cust\bol_6007112_7.pdf");
             bool exists = myFile.Exists;
            return Ok(exists);

Thanks Nuwan

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,420 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,014 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Andriy Bilous 11,821 Reputation points MVP Volunteer Moderator
    2023-04-12T11:35:57.1133333+00:00

    Hello @Nuwan Wickramanayaka If you want to use REST API you should make a request directly to Azure File Share, as in your case file share is mounted via SMB protocol
    Code example

    string connectionString = "<connection_string>";
    
    // Name of the share, directory, and file we'll download from
    string shareName = "sample-share";
    string dirName = "sample-dir";
    string fileName = "sample-file";
    
    // Path to the save the downloaded file
    string localFilePath = @"<path_to_local_file>";
    
    // Get a reference to the file
    ShareClient share = new ShareClient(connectionString, shareName);
    ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
    ShareFileClient file = directory.GetFileClient(fileName);
    
    // Download the file
    ShareFileDownloadInfo download = file.Download();
    using (FileStream stream = File.OpenWrite(localFilePath))
    {
        download.Content.CopyTo(stream);
    }
    

    https://learn.microsoft.com/en-us/dotnet/api/overview/azure/storage.files.shares-readme?view=azure-dotnet

    If you want to access file share from Azure WebApp via SMB protocol, you will need to connect Azure Web App to same VNET with VM and use following code:

    // SMBConfiguration.cs
    using System;
    
    namespace NetFrameworkApp.Controllers
    {
        public class SMBConfiguration
        {
            public String GetSharePath()
            {
                return Environment.GetEnvironmentVariable("SMB_PATH");
            }
    
            public String GetUserName()
            {
                return Environment.GetEnvironmentVariable("SMB_USERNAME");
            }
    
            public String GetPassword()
            {
                return Environment.GetEnvironmentVariable("SMB_PASSWORD");
            }
        }
    }
    

    https://docs.pivotal.io/application-service-windows/2-8/smb-volumes-apps.html


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.