如何从Azure网站计划作业连接远程SFTP [英] How to connect remote SFTP from Azure web site scheduled job

查看:127
本文介绍了如何从Azure网站计划作业连接远程SFTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,它将在AZURE网站上安排为工作. 通过该控制台应用程序,我想连接远程SFTP并获取所有文件,并将它们保存在AZURE网站内的文件夹中.此外,如果有可能,在传输后将其从SFTP中删除.

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.

推荐答案

在这种情况下,首先要使用的最佳和免费选项是 WinSCP .NET程序集.

First of all best and free option to use in this case is WinSCP .NET assembly.

您可以从此处

因此,让我们开始下面的功能:

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);
            }

        }

    }

此功能有什么作用? 它只获取SFTP凭据并登录到SFTP,并列出所有文件名.并将每个文件保存在AZURE网站ftp中.在删除传输的文件之后.

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网站中,它看起来像这样 D:\ home \ site \ wwwroot \ YourFolderName

这篇关于如何从Azure网站计划作业连接远程SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆