Search code examples
asp.netc#-4.0azuresftp

upload file to SFTP from memory stream


How can i upload file to SFTP server from memory stream. Local Path is not available since am uploading the file from azure blob. as i got the information that .NET does not support the SFTP protocol and so tried 3rd party dlls such as 'SharpSSH','Routrek.granados' and 'WinSCP'. But none are suitable for my scenario. ie bite[] or stream are not supported in the put method.

Can anybody suggest me dlls which are free and suits my scenario or a way I can handle this.

Thanks in advance.


Solution

  • You can keep using solutions like WinSCP, but instead of trying to use a MemoryStream / byte[], simply download the file locally first:

    var client = account.CreateCloudBlobClient();
    var container = client.GetContainerReference("temp");
    var blob = container.GetBlobReferenceFromServer("myblob.zip");
    
    // This assumes you're using a Cloud Service and have a local resource called files
    var dropFolder = RoleEnvironment.GetLocalResource("files").RootPath;
    var filePath = Path.Combine(dropFolder, "myblob.zip");
    
    // Download blob to a local resource first.
    using (var fs = new FileStream(filePath, FileMode.Create))
    {
        blob.DownloadToStream(fs);
    }
    
    var proc = new Process();
    proc.StartInfo.FileName = "winscp.com";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardInput = true;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.CreateNoWindow = true;
    proc.Start();
    proc.StandardInput.WriteLine("option batch abort");
    proc.StandardInput.WriteLine("option confirm off");
    proc.StandardInput.WriteLine("open mysession");
    proc.StandardInput.WriteLine("ls");
    proc.StandardInput.WriteLine("put " + filePath);
    proc.StandardInput.Close();