使用WinSCP .NET程序集以流形式访问远程文件内容 [英] Access remote file contents as a stream using WinSCP .NET assembly
问题描述
我试图打开文件以使用WinSCP .NET程序集从SFTP读取,这与将文件从SFTP归档到Azure blob的练习相当.
I am trying to open file to read from SFTP using WinSCP .NET assembly as par to my exercise to archive file from SFTP to Azure blob.
要将Blob上传到Azure,我正在使用
To upload a blob to Azure, I am using
using (var fileStream = inputStream)
{
blockBlob.UploadFromStream(fileStream);
blobUri = blockBlob.Uri.ToString();
}
如何从SFTP服务器上的文件获取流?
How to get the stream from the file on SFTP server?
我使用以下代码管理了SftpClient
来获取流,并且它可以工作,但不幸的是,使用WinSCP .NET程序集无法实现相同的目的.
I managed using SftpClient
to get the stream using the following code and it works but unfortunately not able to achieve the same using WinSCP .NET assembly.
sftpClient.OpenRead(file.FullName)
有人可以帮助我如何使用WinSCP .NET程序集实现相同的功能吗?
Can anyone help me how to achieve the same using WinSCP .NET assembly?
因为我需要使用用户名,密码和私钥连接到SFTP,所以我正在使用WinSCP .NET程序集.
Because I need to connect to SFTP using username, password and privatekey I am using WinSCP .NET assembly.
谢谢
推荐答案
WinSCP .NET程序集
https://winscp.net/tracker/1738
The WinSCP .NET assembly Session
API will support providing the contents of downloaded file using streams only in the upcoming version (5.18):
https://winscp.net/tracker/1738
使用当前版本,您所能做的就是使用 Session.GetFiles
并从那里读取文件:
With the current version, all you can do, is to download the remote file to a local temporary location using the Session.GetFiles
and read the file from there:
// Generate unique file name for the temporary file
string tempPath = Path.GetTempFileName();
// Download the remote file to the temporary location
session.GetFiles("/path/file.ext", tempPath).Check();
try
{
// Open the temporarily downloaded file for reading
using (Stream stream = File.OpenRead(tempPath))
{
// use the stream
blockBlob.UploadFromStream(fileStream);
blobUri = blockBlob.Uri.ToString();
}
}
finally
{
// Discard the temporarily downloaded file
File.Delete(tempPath);
}
这篇关于使用WinSCP .NET程序集以流形式访问远程文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!