我正在尝试编写一组允许应用程序之间进行 SFTP 调用的 Azure 函数。我正在使用 Renci.SshNet 库。
对我来说这一切看起来都是正确的,但是当它运行时我收到错误:
连接到 SFTP 服务器时出错:输入字符串的格式不正确。
这是我设置的对象的快速示例(带有模糊的 var 值):
// Configuration setup for SFTP
SFTPConfiguration config = new SFTPConfiguration
{
Host = Environment.GetEnvironmentVariable("HOST"),
Port = 22, // Hardcoded for testing
Username = Environment.GetEnvironmentVariable("USERNAME"),
Password = Environment.GetEnvironmentVariable("PASSWORD"),
PrivateKeyPath = "[FilePath]"
};
SFTPInterface sftpInterface = new SFTPInterface(config);
sftpInterface.TestConnection();
这是我的 SFTPInterface 构造函数:
public SFTPInterface(SFTPConfiguration config)
{
host = config.Host;
port = config.Port;
username = config.Username;
if (!string.IsNullOrEmpty(config.PrivateKeyPath))
{
// Initialize privateKeyFile with config.Password only if the file requires it
try
{
privateKeyFile = !string.IsNullOrEmpty(config.Password) ?
new PrivateKeyFile(config.PrivateKeyPath, config.Password) :
new PrivateKeyFile(config.PrivateKeyPath);
}
catch(IOException)
{
Console.WriteLine($"[SFTPInterface] Unable to read private key file at {config.PrivateKeyPath}");
}
// Instantiate the SftpClient using the privateKeyFile for authentication
sftp = new SftpClient(host, port, username, privateKeyFile);
}
else if (!string.IsNullOrEmpty(config.Password))
{
// Instantiate the SftpClient using password for authentication
sftp = new SftpClient(host, port, username, config.Password);
}
else
{
throw new ArgumentException("[SFTPInterface] Either a password or a private key path must be provided.");
}
我已经阅读了一些潜在的原因并实施了解决方案,但没有运气:
下面
code
为我工作(稍微修改了你的代码):
using System;
using System.Text;
using Renci.SshNet;
public class RithwikSFTPConfiguration
{
public string Rith_Host { get; set; }
public int Rith_Port { get; set; }
public string Rith_Username { get; set; }
public string Rith_Password { get; set; }
public string Rith_PrivateKeyPath { get; set; } // Directly provide private key
}
public class Rith_SFTP
{
private string rith_host;
private int rith_port;
private string rith_username;
private SftpClient rith_sftp;
private PrivateKeyFile privateKeyFile;
public Rith_SFTP(RithwikSFTPConfiguration config)
{
rith_host = config.Rith_Host;
rith_port = config.Rith_Port;
rith_username = config.Rith_Username;
if (!string.IsNullOrEmpty(config.Rith_PrivateKeyPath))
{
try
{
privateKeyFile = !string.IsNullOrEmpty(config.Rith_Password) ?
new PrivateKeyFile(config.Rith_PrivateKeyPath, config.Rith_Password) :
new PrivateKeyFile(config.Rith_PrivateKeyPath);
}
catch (IOException)
{
Console.WriteLine($"Hello Rithwik, Unable to read private key file at {config.Rith_PrivateKeyPath}");
}
rith_sftp = new SftpClient(rith_host, rith_port, rith_username, privateKeyFile);
}
else if (!string.IsNullOrEmpty(config.Rith_Password))
{
rith_sftp = new SftpClient(rith_host, rith_port, rith_username, config.Rith_Password);
}
else
{
throw new ArgumentException("Hello Rithwik, Either a password or a private key path must be provided.");
}
}
public void TestConnection()
{
try
{
rith_sftp.Connect();
Console.WriteLine("Hello Rithwik, SFTP connection successful.");
rith_sftp.Disconnect();
}
catch (Exception ex)
{
Console.WriteLine($"Hello Rithwik, Error connecting to SFTP server: {ex.Message}");
}
}
}
public class Rith_SFTPFunction
{
public static void Run()
{
RithwikSFTPConfiguration rith_conf = new RithwikSFTPConfiguration
{
Rith_Host = "rith32.blob.core.windows.net",
Rith_Port = 22,
Rith_Username = "rith32.rithwik.user1",
Rith_Password = "dwCMI1UzB7XcIt5o",
Rith_PrivateKeyPath = @"C:\Users\Downloads\key1"
};
Rith_SFTP rith_s = new Rith_SFTP(rith_conf);
rith_s.TestConnection();
}
public static void Main(string[] args)
{
Rith_SFTPFunction.Run();
}
}
这里,
用户名应为
storageaccountname.conatinername.username
输入密码
提供您的密钥对路径
主机名应为
storageaccountname.blob.core.windows.net
Output: