Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access remote file contents as a stream using WinSCP .NET assembly

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.

To upload a blob to Azure, I am using

using (var fileStream = inputStream)
{
    blockBlob.UploadFromStream(fileStream);
    blobUri = blockBlob.Uri.ToString();
}

How to get the stream from the file on SFTP server?

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)

Can anyone help me how to achieve the same using WinSCP .NET assembly?

Because I need to connect to SFTP using username, password and privatekey I am using WinSCP .NET assembly.

Thanks

like image 209
Mukil Deepthi Avatar asked Oct 08 '15 11:10

Mukil Deepthi


1 Answers

The WinSCP .NET assembly supports providing the contents of a remote file using streams using the Session.GetFile method:

using (Stream stream = session.GetFile("/path/file.ext"))
{
    blockBlob.UploadFromStream(stream);
}
like image 68
Martin Prikryl Avatar answered Sep 24 '22 00:09

Martin Prikryl