Muk*_*thi 5 c# winscp winscp-net azure-blob-storage
我试图打开文件以使用WinSCP .NET程序集从SFTP读取,这与将文件从SFTP存档到Azure blob的练习相当。
要将blob上传到Azure,我正在使用
using (var fileStream = inputStream)
{
blockBlob.UploadFromStream(fileStream);
blobUri = blockBlob.Uri.ToString();
}
Run Code Online (Sandbox Code Playgroud)
如何从SFTP服务器上的文件获取流?
我设法使用SftpClient下面的代码来获取流,并且可以工作,但是不幸的是,使用WinSCP .NET程序集无法实现相同的目的。
sftpClient.OpenRead(file.FullName)
Run Code Online (Sandbox Code Playgroud)
谁能帮我使用WinSCP .NET程序集实现相同的目的?
因为我需要使用用户名,密码和私钥连接到SFTP,所以我使用的是WinSCP .NET程序集。
谢谢
WinSCP .NET 程序集仅在当前 beta 版本 (5.18) 中支持使用流提供远程文件的内容,Session.GetFile方法如下:
using (Stream stream = session.GetFile("/path/file.ext"))
{
blockBlob.UploadFromStream(stream);
}
Run Code Online (Sandbox Code Playgroud)
使用当前的稳定版本,您所能做的就是使用Session.GetFileToDirectory(或类似的)将远程文件下载到本地临时位置并从那里读取文件:
// Download the remote file to the temporary location
var transfer = session.GetFileToDirectory("/path/file.ext", Path.GetTempPath());
try
{
// Open the temporarily downloaded file for reading
using (Stream stream = File.OpenRead(transfer.Destination))
{
// use the stream
blockBlob.UploadFromStream(stream);
blobUri = blockBlob.Uri.ToString();
}
}
finally
{
// Discard the temporarily downloaded file
File.Delete(transfer.Destination);
}
Run Code Online (Sandbox Code Playgroud)