微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 SSIS/C# 中的两个远程 FTP 和 SFTP 服务器之间传输文件,然后将源文件归档到另一个目录

如何解决在 SSIS/C# 中的两个远程 FTP 和 SFTP 服务器之间传输文件,然后将源文件归档到另一个目录

我已经为此苦苦挣扎了一段时间,我真的不明白如何通过使用 SSIS 来解决这种情况(我正在考虑在 C# 中创建一个脚本任务)

这是我的问题:每天都会创建一个文件并将其存储在某个 特定服务器中的目录(我可以使用 FTP 访问它 客户端 - 服务器应用程序中的协议,例如 FileZilla 或任何 其他程序)。我想将该文件传输到使用 SFTP 协议的其他服务器。然后,一旦它成功地放置在该服务器中,将原始文件移动到源 FTP 服务器上的不同文件夹中。

看看这个例子:

我的文件存储在源 FTP 服务器上的这个目录中:

enter image description here

我需要将文件 myfiletest.csv 传输到不同的 SFTP 服务器。路径是:SecondServer/placehere

image

最后,文件传输完毕后,我想将源文件移动到文件Process

enter image description here


一直在尝试将文件从第一个服务器复制并移动到第二个服务器,但没有成功,我现在有点迷茫。

能帮我解决吗?


这就是我试图从 FTP 服务器读取文件的方式(我无法正确读取它,甚至无法将其保存在我的本地环境中)。

 public void Main()
 {
    var directory =
        new DirectoryInfo(Dts.Variables["User::FolderPath"].Value.ToString());

    FileInfo[] files = directory.GetFiles();
    DateTime lastModified = DateTime.MinValue;

    foreach (FileInfo file in files)
    {
        if (file.LastWriteTime > lastModified)
        {
            lastModified = file.LastWriteTime;
            Dts.Variables["User::FileName"].Value = file.ToString();
        }
    }

     MessageBox.Show(Dts.Variables["User::FileName"].Value.ToString());


     Dts.TaskResult = (int)ScriptResults.Success;
 }

解决方法

您的代码未访问任何服务器。它不使用 FTP 或 SFTP。它适用于本地文件。


无论如何,如果 FTP 和 SFTP 是您唯一可用的接口,则无法直接将文件从 FTP 复制到 SFTP 服务器。

您必须从 FTP 服务器下载文件,然后将它们上传到 SFTP 服务器。


您可以将下载的文件直接流式上传,避免将文件存储到临时本地文件中。

如果您想使用内置 .NET FTP 实现 (WebClient/FtpWebRequest) 和本机 .NET SFTP 库(如 SSH.NET),您可以执行以下操作:

var ftpClient = new WebClient();
ftpClient.Credentials = new NetworkCredential("ftpuser","ftppass");
var ftpUrl = "ftp://ftp.example.com/ftp/path/file.csv";
using (var ftpStream = ftpClient.OpenRead(ftpUrl))
using (var sftpClient = new SftpClient("sftp.example.com","sftpuser","sftppass"))
{
    sftpClient.UploadFile(ftpStream,"/sftp/path/file.csv");
}

移动处理后的文件:

FtpWebRequest ftpRequest = new FtpWebRequest(ftpUrl);
ftpRequest.Method = WebRequestMethods.Ftp.Rename;
ftpRequest.RenameTo = "/ftp/path/processed/file.csv";
ftpRequest.GetResponse(); 

如果是那两个 SFTP 服务器,我的 WinSCP .NET assembly 会更容易一些。什么不是本机 .NET 库,但它can be used from SSIS。使用 WinSCP,代码将类似于:

// Setup session options
SessionOptions sftpSessionOptions1 = new SessionOptions
{
    Protocol = Protocol.Sftp,HostName = "sftp1.example.com",UserName = "username1",Password = "password",};

SessionOptions sftpSessionOptions2 = new SessionOptions
{
    Protocol = Protocol.Sftp,HostName = "sftp2.example.com",UserName = "username2",SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

using (Session sftpSession1 = new Session())
using (Session sftpSession2 = new Session())
{
    // Connect to SFTP 1
    ftpSession1.Open(sftpSessionOptions1);

    // Get list of files in the FTP directory
    string sftpRemoteDir1 = "/ftp/remote/path";
    RemoteDirectoryInfo dirInfo = sftpSession1.ListDirectory(sftpRemoteDir1);

    // Select the most recent file
    RemoteFileInfo latest =
        dirInfo.Files
            .Where(file => !file.IsDirectory)
            .OrderByDescending(file => file.LastWriteTime)
            .First();

    // Connect to SFTP
    sftpSession2.Open(sftpSessionOptions2);

    string sftpRemoteDir2 = "/sftp/remote/path";
    string sftpRemotePath2 = RemotePath.Combine(sftpRemoteDir2,latest.Name);

    // Transfer from SFTP 1 to SFTP 2
    using (Stream downloadStream = sftpSession1.GetFile(latest.FullName))
    {
        sftpSession2.PutFile(downloadStream,sftpRemotePath2);
    }

    // Move the source file to the "processed" folder
    string processedDir = "/sftp/remote/path/processed";
    string processedPath = RemotePath.Combine(processedDir,latest.Name);
    ftpSession.MoveFile(latest.FullName,processedPath);
}

未经测试。需要 WinSCP 5.19 或更新版本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。