0

我有一个 Azure 函数,它使用混合连接从本地服务器读取文件。

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

运行时,函数应用程序将上述文件路径转换为“C://azurefuncpath/servername/sample.csv”并输出错误。提供文件路径的正确方法应该是什么,以便函数应用程序使用服务器路径而不是本地路径。

更新 1 我尝试了下面的代码来查看 Azure Function 是否能够使用混合连接访问文件夹

var files = Directory.GetFiles(@"\\ServerName\FolderName", "*.*", SearchOption.AllDirectories);    
    foreach (var file in files)
    {
        log.LogInformation(file);
    }
    return new OkResult();
运行不成功,输出为 2021-12-02T09:19:25Z [信息] 捕获异常:拒绝访问路径 '\servername\foldername'。2021-12-02T09:19:25Z [信息] 已执行 'ImportCsV '(成功,Id=29b1e6c2-310b-416e-9795-a6a57e6a5ef7,持续时间=23ms)2021-12-02T09:19:30Z [信息] 执行 'ImportCsV'(原因='此函数是通过主机 API 以编程方式调用的。 ', Id=785af185-1524-45e2-aec3-60999e573c1d)2021-12-02T09:19:30Z [信息] C# 定时器触发函数执行于:12/2/2021 9:19:30 AM2021-12-02T09:19 :30Z [信息] 捕获异常:访问路径 '\servername\foldername' 被拒绝。2021-12-02T09:19:30Z [信息] 执行 'ImportCsV' (成功,Id=785af185-1524-45e2-aec3- 60999e573c1d,持续时间=25ms)

我在本地服务器中看到混合连接显示它处于连接状态。我应该使用其他方式来执行此操作吗?

4

1 回答 1

0

我找到了使用 SFTP 和混合连接管理器 (HCM) 实现此目的的解决方法。

第 1 步:在 OnPremise 服务器中设置 HCM(可以在任何 Intranet 服务器中设置)参考此链接。现在这应该启用从 Azure 到 OnPremise 的安全连接,反之亦然。

第 2 步:使用 SFTP 代码访问文件。我为此使用了 SSH.NET 包。下面是代码片段

            // 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

于 2022-01-04T17:04:47.210 回答