【问题标题】:Transferring file between two remote FTP and SFTP servers in SSIS/C# and archiving source files to another directory afterwards在 SSIS/C# 中的两个远程 FTP 和 SFTP 服务器之间传输文件,然后将源文件归档到另一个目录
【发布时间】:2021-06-29 09:54:43
【问题描述】:

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

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

看看这个例子:

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

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

最后,一旦文件被传输,我想将源文件移动到文件夹Process


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

可以帮我解决吗?


这就是我尝试从 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;
 }

【问题讨论】:

    标签: c# .net ssis ftp sftp


    【解决方案1】:

    您的代码不访问任何服务器。它不使用 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",
        Password = "password",
        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 或更高版本。

    【讨论】:

    • 你太棒了!它实际上帮助了我,对我来说很有意义!!
    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2021-12-31
    • 2022-08-08
    相关资源
    最近更新 更多