【发布时间】:2022-06-23 20:41:43
【问题描述】:
我正在开发 .NET 6.0 应用程序以及 SFTP Renci.SshNet 库。我有从字符串中隐藏的数据流。我正在尝试UploadFile(Stream, filename),但出现错误。不知道我从拼图中错过了什么
error
at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Action`1 uploadCallback)
在
我在代码中给出的文件名在 SFTP 服务器上不存在,因为我希望自动创建
Upload Stream
public bool UploadStream(Tenant destinationTenant, Stream stream)
{
string fileStream = string.Empty;
bool isFileUploaded = false;
var remoteFilePath = destinationTenant.RemoteDirectoryPath;
var sftpClient = sftpClients.FirstOrDefault(_ => _.Key == destinationTenant.TenantId.ToString()).Value;
if (sftpClient == null)
{ Console.Write($"Unable To Retrieve SFTP Client Configuration; Dated [{DateTime.UtcNow}] "); }
try
{
if (sftpClient != null)
{
sftpClient.Connect();
if (sftpClient.IsConnected)
{
sftpClient.ChangeDirectory(remoteFilePath);
sftpClient.UploadFile(stream, "/myfile.txt");
}
}
}
catch (Exception ex)
{
throw;
}
finally
{
if (sftpClient != null)
{ sftpClient.Disconnect(); }
}
return isFileUploaded;
}
}
【问题讨论】: