SftpFileStream path question
I am connecting to a 3rd party SFTP server. I need to download all files in a folder with download resume functionality. After getting a listing of files in the current folder from the remote server, I am unable to create a SftpFileStream object for them using SftpClient.OpenRead. When I past SftpFile.Name or SftpFile.FullName as the path I get "Folder not found" error. But it is there or it wouldn't be in the list of files from SftpClient.ListDirectory. Am I not understanding the correct use of path in OpenRead?
Code from Visual Studio 2015 console app. SSH.NET from NuGet.
Error is on line: Dim sffsRemoteFile As SftpFileStream = sftpClient.OpenRead(sfFile.Name)
Message: Folder not found: LOG_us_prin_multi_replica_2016091806.SQB
Stack Trace: at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError) at Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize, Boolean useAsync) at Renci.SshNet.Sftp.SftpFileStream..ctor(ISftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize) at Renci.SshNet.SftpClient.OpenRead(String path) at SSHFTP.SSHFTP.Download_Many_With_Resume(String pServer, String pUser, String pPass, String pRmPath, String pLcPath, Int32 pPort, Int32 pBlock, Int32 pTimeout) in C:\Users\Will\Documents\Visual Studio 2015\Projects\SSHFTP\SSHFTP\SSHFTP.vb:line 318
Private Sub Download_Many_With_Resume(pServer As String, pUser As String, pPass As String, pRmPath As String, pLcPath As String, pPort As Integer, pBlock As Integer, pTimeout As Integer)
Dim boolErrorLogged As Boolean = False
Dim strStatusMsg As String = "Init"
Dim strTempFileSuffix As String = ".filepart"
Dim sftpClient As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(pServer, pPort, pUser, pPass)
Try '// Main Try
sftpClient.BufferSize = pBlock
If (pTimeout > 0) Then
sftpClient.KeepAliveInterval = New TimeSpan(0, 0, pTimeout)
End If '// End If (mintTimeout > 0) Then
sftpClient.Connect()
sftpClient.ChangeDirectory(pRmPath) '// In test case /prinftpuser/dbreplication/Logs/
'// List all files and folders in the remote path...WRG
Dim lofListing As List(Of SftpFile) = sftpClient.ListDirectory(".").ToList()
If lofListing IsNot Nothing AndAlso lofListing.Count() > 2 Then
'// Filter the listing for files based on dos wildcard from command line...WRG
Dim lofFilteredList As List(Of SftpFile) = lofListing.FindAll(AddressOf FilterByMask)
For Each sfFile As SftpFile In lofFilteredList
Dim strLocalTempFile As String = pLcPath + sfFile.Name + strTempFileSuffix
Dim lngOffset As Long = 0
If File.Exists(strLocalTempFile) Then
'// Resume a past download...WRG
lngOffset = My.Computer.FileSystem.GetFileInfo(strLocalTempFile).Length
End If '// End If File.Exists(strLocalTempFile) Then
If Not File.Exists(pLcPath + sfFile.Name) Then
Dim sLocalFile As Stream = File.Open(strLocalTempFile, FileMode.OpenOrCreate, FileAccess.Write)
strStatusMsg = "File : " + sfFile.FullName
Try '// Write File Try
Dim intReadPos As Integer = 0
Dim lngByteCount As Long = lngOffset
Dim bFileChunk(mintBlockSize) As Byte
Dim sffsRemoteFile As SftpFileStream = sftpClient.OpenRead(sfFile.Name)
'// Note that all code past this point is untested as I can't get OpenRead to give me a stream...WRG
'// For a resume jump the stream to the point we need to resume from...WRG
If lngOffset > 0 Then
sffsRemoteFile.Position = lngOffset
End If
Do '// Main read loop...WRG
If sffsRemoteFile.CanRead Then
intReadPos = sffsRemoteFile.Read(bFileChunk, 0, bFileChunk.Length)
sLocalFile.Write(bFileChunk, 0, intReadPos)
Else
intReadPos = 0
End If
lngByteCount += intReadPos
Loop Until intReadPos = 0 '// End Main read loop...WRG
sffsRemoteFile.Close()
'// Check to see if the whole file been downloaded...WRG
If lngByteCount <> sfFile.Attributes.Size Then
'// Byte counts don't match, download is not complete...WRG
boolErrorLogged = True
strStatusMsg = "Partial write (" + sfFile.Name + ")"
Else
'// All is well...WRG
strStatusMsg = "File write (" + sfFile.Name + ") completed"
End If
Catch ex As Exception
boolErrorLogged = True
strStatusMsg += " write file error :" + ex.Message
Finally '// Finally Write File Try
sLocalFile.Flush()
sLocalFile.Close()
sLocalFile = Nothing
End Try '// End Write File Try
If File.Exists(strLocalTempFile) And Not boolErrorLogged Then
'// Set the file's modified and last access timestamp. Leave create alone to track download timestamp...WRG
File.SetLastAccessTime(strLocalTempFile, sfFile.Attributes.LastAccessTime)
File.SetLastWriteTime(strLocalTempFile, sfFile.Attributes.LastWriteTime)
'// Rename the now completed download...WRG
My.Computer.FileSystem.RenameFile(strLocalTempFile, sfFile.Name)
End If '// End If File.Exists(strLocalTempFile) And Not boolErrorLogged Then
Else
strStatusMsg = "File already exists, can not download " + sfFile.Name
End If '// End If Not File.Exists(pLcPath + sfFile.Name) Then
System.Console.WriteLine(strStatusMsg)
strStatusMsg = ""
Next '// End For Each sfFile As SftpFile In lofFilteredList
Else
boolErrorLogged = True
strStatusMsg = "File not found, " + pRmPath + mstrFileMask
End If '// End If lofListing IsNot Nothing AndAlso lofListing.Count() > 2 Then
Catch ex As Exception
boolErrorLogged = True
strStatusMsg = "Download error :" + ex.Message + " " + mstrFileMask
Finally '// Main Try
If sftpClient.IsConnected Then
sftpClient.Disconnect()
End If '// End If sftpClient.IsConnected Then
sftpClient.Dispose()
sftpClient = Nothing
'// Display any final messages...WRG
System.Console.WriteLine(strStatusMsg)
End Try '// Main Try
End Sub
One other item of note, on the customer's WS_FTP server they apparently have their root directory for the user homed on a network share. That seems to be the same as @lochnar187 's scenario as well.
To work around the issue we ended up having to create a symbolic directory link with mklink underneath default directory that ws_ftp created for the user and set their home directory to that symbolic link.
@lochnar187: Do you still run into this issue? If so, I'll make some time to look into this.
Sorry it took me a bit to get back to this. Everything looks good now. I have not been able to reproduce the error with beta2.
Thanks much!
Hi, I am unable to download the file. Getting error 'Renci.SshNet.Common.SftpPathNotFoundException: No such file at' Can anyone help me in this ?
Hi @drieseng, I came across a situation where i have to upload file to my client server, used the following code and receiving Exception =>Renci.SshNet.Common.SftpPathNotFoundException: File not found at Renci.SshNet.Sftp.SftpSession.RequestOpenDir(String path, Boolean nullOnError) at Renci.SshNet.Sftp.SftpSession.ChangeDirectory(String path) at Renci.SshNet.SftpClient.ChangeDirectory(String path) at RxLincFTP.sftp.UploadSFTPFile(String host, String username, String password, String sourcefile, String destinationpath, Int32 port) /**********************************************My code
private void TransferFileSSH(string ftpHost, int ftpPort, string ftpUserName, string ftpPassword)
{
try
{
log.Info("Enters TransferFileSSH");
using (var sftp = new SftpClient(ftpHost, ftpPort, ftpUserName, ftpPassword))
{
sftp.Connect();
log.Info("Connected.........");
//sftp.CreateDirectory(dropOutPath); //Create folder if necessary else skip
log.Info("dropOutPath => " + dropOutPath);
var files = Directory.GetFiles(pickUpPath);
foreach (var file in files)
{
string remoteFileName = file;
log.Info("remoteFileName => " + remoteFileName);
using (Stream file1 = new FileStream(file, FileMode.Open))
{
remoteFileName = remoteFileName.Substring(48); //Take out the filename
log.Info("remoteFileName Take out the filename => " + remoteFileName);
log.Info(string.Format("{0}/{1}", dropOutPath, remoteFileName));
sftp.UploadFile(file1, dropOutPath + "/" + remoteFileName, null);
sftp.Disconnect();
sftp.Dispose();
log.Info("File uploaded successfully");
MessageBox.Show("Sucessful?", "Rx Linc", MessageBoxButtons.OK);
}
}
}
}
catch (Exception Ex)
{
string exception = Ex.ToString();
log.Info("caught in exception => " + exception);
}
}
/***********************************
config value
Thanks !
Any help will be great.....Thanks Deep
Hello, I'm experiencing the same issue. I'm wondering the root cause could be that the file has write permission but not read permissions. Another aspect is we don't have an specific certificate for the ssh process so I'm wonder if that could make a difference.
has anyone being able to work around the issue. I'm using the latest version.
I created my own locals sftp server and the code works fine, I'd bet the problem is the files with write but not read permissions. Who ever has this issue check the files permissions.