SFTP Upload using sshnet library - Handling Connection Issues

10,506

When transferring files, The SftpClient.OperationTimeout determines how long the library waits for a single buffer read/write operation (64 KB max by default, see SftpClient.BufferSize). Not how long it waits for whole transfer to complete.

So you can safely change it from the default "infinite" to some meaningful value.

Share:
10,506
TommyGunn32
Author by

TommyGunn32

.NET, PHP, Python, SQL

Updated on June 04, 2022

Comments

  • TommyGunn32
    TommyGunn32 almost 2 years

    Using Renci's sshnet library, I am unable to handle connection issues while uploading a file.

    If I set a break point on UploadFile, disable my connection, and let it run, it just hangs on that line. These are potentially big files on slow connections, so adding an OperationTimeout would be difficult.

    Also, this works fine over and over on consistent connections. Any ideas? Thanks!

    AuthenticationMethod[] methods = new AuthenticationMethod[1];
    methods[0] = new PasswordAuthenticationMethod(username, pwd);
    
    var con = new ConnectionInfo(Config.RemoteHostName, username, methods);
    con.Timeout = new TimeSpan(0, 0, 0, 30);
    var client = new SftpClient(con);
    
    client.Connect();
    
    string fullFilePath = string.Format("{0}{1}", Config.RemoteFilePath, filename);
    client.BufferSize = 15000;
    client.UploadFile(new MemoryStream(buffer), fullFilePath);
    client.Disconnect();