Search code examples
c#file-uploadsftpssh.net

SSH.Net Upload file to SFTP server returns exception with "Failure" message without clear details


I am trying to upload an XML file to SFTP server using SSH.NET. (.NetCore 3.1 / VS 2019 / C#)

The remote folder path to upload the file is "/testin/" which we have been confirmed that we have enough write permission to upload files to this folder. I am able to connect and authorized to SFTP server on this connection. However, when I try to upload a file, SSH.net return only "Failure" in the exception message with no other clear details. I am able to download files with no issue. By checking the summary details of "Upload" function from SSH.NET as below, it specifies that it would return clear error message for permissions and so on. I did some search on internet with not much success.

    // Summary:
    //     Uploads stream into remote file.
    //
    // Parameters:
    //   input:
    //     Data input stream.
    //
    //   path:
    //     Remote file path.
    //
    //   canOverride:
    //     if set to true then existing file will be overwritten.
    //
    //   uploadCallback:
    //     The upload callback.
    //
    // Exceptions:
    //   T:System.ArgumentNullException:
    //     input is null.
    //
    //   T:System.ArgumentException:
    //     path is null or contains only whitespace characters.
    //
    //   T:Renci.SshNet.Common.SshConnectionException:
    //     Client is not connected.
    //
    //   T:Renci.SshNet.Common.SftpPermissionDeniedException:
    //     Permission to upload the file was denied by the remote host.
    //     -or-
    //     A SSH command was denied by the server.
    //
    //   T:Renci.SshNet.Common.SshException:
    //     A SSH error where System.Exception.Message is the message from the remote host.
    //
    //   T:System.ObjectDisposedException:
    //     The method was called after the client was disposed.
    //
    // Remarks:
    //     Method calls made by this method to input, may under certain conditions result
    //     in exceptions thrown by the stream.
    public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null);

Here is my code: Any idea?

       using (var sftp = new SftpClient(Host, TcpPort, Username, Password))
        {
            try
            {
                sftp.Connect();

                var localFile = Path.Combine(LocalUploadPath + LocalFilename);

               // var path = $"{localFile.Replace(@"\", "/")}";

                using (var fs = File.OpenRead(localFile))
                {
                    sftp.UploadFile(fs, RemoteUploadRootPath, true);
                }
            }
            catch (Exception ex)
            {
                sftp.Disconnect();

                throw new Exception($"{ex.Message} {ex.InnerException}"
            }
            finally
            {
                sftp.Disconnect();
            }
        }

enter image description here


Solution

  • I found the issue, it was simple enough, in the upload path, the file name was not specified in the file path. :| This link may worth to share for SSH.NET with some good examples: https://csharp.hotexamples.com/examples/Renci.SshNet.Sftp/SftpUploadAsyncResult/Update/php-sftpuploadasyncresult-update-method-examples.html

    Here is the updated code that works fine:

            using (var sftp = new SftpClient(Host, TcpPort, Username, Password))
            {
                try
                {
                    sftp.Connect();
    
                    var localFile = Path.Combine(LocalUploadPath + LocalFilename);
                    var remoteFilePath = Path.Combine("/testin/" + LocalFilename);
    
                    using (var fs = File.OpenRead(localFile))
                    {
                        sftp.UploadFile(fs, remoteFilePath, true);//, UploadCallBack);
                    }
                }
                catch (Exception ex)
                {
                    sftp.Disconnect();
    
                    throw new Exception($"{ex.Message} {ex.InnerException}");
                }
                finally
                {
                    sftp.Disconnect();
                }
            }