0

I have a Azure Function which reads a file from OnPremise server using Hybrid Connection.

using (var csvStreamReader = new StreamReader("\\servername\sample.csv"))
...

On run the function app converts the above filepath to "C://azurefuncpath/servername/sample.csv" and errors out. What should be the right way to provide the file path so the function app to use server path instead of local path.

Update 1 I tried a below code to see if Azure Function is able to access the folder using Hybrid Connection

var files = Directory.GetFiles(@"\\ServerName\FolderName", "*.*", SearchOption.AllDirectories);    
    foreach (var file in files)
    {
        log.LogInformation(file);
    }
    return new OkResult();
The run was unsuccessful and output is 2021-12-02T09:19:25Z [Information] Caught exception: Access to the path '\servername\foldername' is denied.2021-12-02T09:19:25Z [Information] Executed 'ImportCsV' (Succeeded, Id=29b1e6c2-310b-416e-9795-a6a57e6a5ef7, Duration=23ms)2021-12-02T09:19:30Z [Information] Executing 'ImportCsV' (Reason='This function was programmatically called via the host APIs.', Id=785af185-1524-45e2-aec3-60999e573c1d)2021-12-02T09:19:30Z [Information] C# Timer trigger function executed at: 12/2/2021 9:19:30 AM2021-12-02T09:19:30Z [Information] Caught exception: Access to the path '\servername\foldername' is denied.2021-12-02T09:19:30Z [Information] Executed 'ImportCsV' (Succeeded, Id=785af185-1524-45e2-aec3-60999e573c1d, Duration=25ms)

I see in onpremise server the Hybrid connection shows it's in Connected state. Should i be using some other way to do this operation?

prvn
  • 406
  • 3
  • 7
  • 24

1 Answers1

0

I found a workaround to achieve this using SFTP and Hybrid Connection Manager(HCM).

Step 1: Setup HCM in the OnPremise server(can be setup in any of the intranet server) refer this link. Now this should enable a secure connection from Azure to OnPremise and vice versa.

Step 2: Use SFTP code to access file. I used SSH.NET package for this. Below is the code snippet

            // Host is the HCM name from azure, i created with server name
            // Port is 25 for SFTP
            // Username password is the SFTP creds
            using (SftpClient client = new SftpClient(Host, Port, UserName, Password))
            {
                client.Connect();
                ..
                
                // filepath is the local path of file in server
                // eg: D://Export/sample.csv
                client.DownloadFile(filepath, stream);
                stream.Seek(0, SeekOrigin.Begin);
                
                // Now stream will have the onpremise filestream
                // read all the data needed and process further
prvn
  • 406
  • 3
  • 7
  • 24