I have one console app which will be scheduled as job in AZURE web site. From that console app I want to connect remote SFTP and get all files and save them in my folder inside AZURE web site.Also if it possible remove them from SFTP after transfer.
Asked
Active
Viewed 2,085 times
4
-
1See my article [SFTP/FTPS file transfers in Microsoft Azure WebJob](http://winscp.net/eng/docs/guide_microsoft_azure_webjob_sftp). – Martin Prikryl May 21 '15 at 06:56
1 Answers
5
First of all best and free option to use in this case is WinSCP .NET assembly.
You can download it from here
So lets start this is the function:
public static void GetSftp(string host, string user, string password, int port, string source, string dest, string remoteDest)
{
Directory.CreateDirectory(dest);
var winScpSessionOptions = new SessionOptions
{
HostName = host,
Password = password,
PortNumber = port,
UserName = user,
Protocol = Protocol.Sftp,
GiveUpSecurityAndAcceptAnySshHostKey = true
};
var session = new Session();
session.Open(winScpSessionOptions);
var remoteDirInfo = session.ListDirectory(remoteDest);
foreach (RemoteFileInfo fileInfo in remoteDirInfo.Files)
{
if (fileInfo.Name.Equals(".") || fileInfo.Name.Equals("..")) { continue; }
Console.WriteLine("{0}", remoteDest + fileInfo.Name);
try
{
var x = remoteDest +"/"+ fileInfo.Name;
var y = dest +"\\"+ fileInfo.Name;
var result = session.GetFiles(x, y);
if (!result.IsSuccess)
{
}
else
{
session.RemoveFiles(remoteDest +"/"+ fileInfo.Name);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
What this function does? It just gets SFTP credentials and login to SFTP .And lists all file names .And saves each file in AZURE web site ftp.After it removes transferred file.
- source is SFTP folder
- Destination where you want to transfer files from SFTP.In AZURE web sites it looks like this D:\home\site\wwwroot\YourFolderName

Martin Prikryl
- 188,800
- 56
- 490
- 992

RASKOLNIKOV
- 732
- 2
- 9
- 20
-
11) Please do not recommend use of beta release. Always use the latest stable version of WinSCP. 2) Note that your are using WinSCP .NET assembly, not just WinSCP. 3) Do not set [`GiveUpSecurityAndAcceptAnySshHostKey`](http://winscp.net/eng/docs/library_sessionoptions) unless you do not care about security and you really know what you are doing. See [Where do I get SSH host key fingerprint to authorize the server?](http://winscp.net/eng/docs/faq_hostkey) – Martin Prikryl May 21 '15 at 06:52