简体   繁体   English

如何从Azure网站计划作业连接远程SFTP

[英]How to connect remote SFTP from Azure web site scheduled job

I have one console app which will be scheduled as job in AZURE web site. 我有一个控制台应用程序,将安排在AZURE网站上的工作。 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. 从该控制台应用程序我想连接远程SFTP并获取所有文件并将其保存在AZURE网站内的我的文件夹中。如果可以在转移后从SFTP中删除它们。

First of all best and free option to use in this case is WinSCP .NET assembly . 首先,在这种情况下使用的最佳和免费选项是WinSCP .NET程序集

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. 它只获取SFTP凭据并登录到SFTP。并列出所有文件名。并将每个文件保存在AZURE网站ftp中。删除传输的文件后。

  • source is SFTP folder source是SFTP文件夹
  • Destination where you want to transfer files from SFTP.In AZURE web sites it looks like this D:\\home\\site\\wwwroot\\YourFolderName 要从SFTP传输文件的目的地。在AZURE网站中,它看起来像这样D:\\ home \\ site \\ wwwroot \\ YourFolderName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM