问题描述
我有一个控制台应用程序,它将被安排为 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:homesitewwwrootYourFolderName
这篇关于如何从 Azure 网站计划作业连接远程 SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!