I didn't get any
FileTransferred
events from using
GetFile()
(note, not
GetFiles
- just
GetFiles
because I need it to go into a stream).
According to the documentation this should trigger an event.
Not sure if the file sizes have anything to do with this issue - the files I'm transferring are very small ~ 1–2KB, and yes, I'm on SFTP.
The breakpoint at
WinSCPFileTransferred
never gets triggered despite files being downloaded and transferred successfully.
My code is as follows:
public bool FetchFiles()
{
SendMsg = false;
try
{
if (!SetConnectionParameters())
{
nlog.Error("FTPService: Failed configuring Session Parameters for {name}", currEntity.name);
return false;
}
using (Session session = new Session())
{
session.ExecutablePath = Path.Combine(CurrContext.FunctionAppDirectory, "winscp.exe");
session.DebugLogPath = null; //Null means no log path (Path to store assembly debug log)
session.SessionLogPath = null; //Null means no log path (Path to store session log path)
session.FileTransferred += WinSCPFileTransferred;
try
{
if (!session.Opened)
{
session.Open(sessionOptions);
}
var transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary,
OverwriteMode = OverwriteMode.Overwrite,
};
nlog.Info("FTPService: Session Open for {name}. About to Get files", currEntity.name);
GetFilesAndUploadAsync(session);
}
catch (Exception ex)
{
nlog.Error(ex, "FTPService: Exception Opening Session Parameters for {name}.", currEntity.name);
return false;
}
}
}
catch (Exception ex)
{
nlog.Error(ex);
return false;
}
return true;
}
private void GetFilesAndUploadAsync(Session conn)
{
try
{
string filePath = null;
//Get a List of Files in Directory - seems to be +1 of the actual file count...
RemoteDirectoryInfo directory = conn.ListDirectory(currEntity.remotefolder);
Stream stream;
nlog.Debug("Number of files in remote directory:{count}",directory.Files.Count-1);
foreach (RemoteFileInfo fileInfo in directory.Files)
{
if (!fileInfo.Name.Equals(".") & !fileInfo.Name.Equals(".."))
{
nlog.Debug(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
filePath = currEntity.remotefolder + "/" + fileInfo.Name;
//only supported for SFTP - otherwise will throw exception!
//GetFile/Putfile from WinSCP returns a custom and very limited implementation of Stream.
//it CANNOT be seeked i.e. SetLength/Position not allowed
stream = conn.GetFile(filePath);
if (PushFileToAzureBlobContainer(stream, fileInfo.Name))
{
try
{
stream.Dispose();
conn.RemoveFile(filePath);
}
catch (Exception ex)
{
nlog.Error(ex, "FTPService: Exception removing file {filename} from remote directory for {name}.", fileInfo.Name, currEntity.name);
}
}
stream.Dispose();
}
}
}
catch (Exception ex)
{
nlog.Error(ex, "FTPService: Exception listing remote directory for {name}.", currEntity.name);
}
}