在 SSIS/C# 中的两个远程 FTP 和 SFTP 服务器之间传输文件,然后将源文件归档到另一个目录
Transferring file between two remote FTP and SFTP servers in SSIS/C# and archiving source files to another directory afterwards
我已经为此苦苦挣扎了一段时间,我真的不明白如何使用 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;
}
您的代码没有访问任何服务器。它不使用 FTP 或 SFTP。它适用于 local 文件。
无论如何,如果 FTP 和 SFTP 是您唯一可用的接口,则无法直接将文件从 FTP 复制到 SFTP 服务器。
您必须从FTP服务器下载文件,然后将它们上传到SFTP服务器。
用于在 FTP 服务器上查找最新文件:
FTP下载,见
SFTP上传见C# SFTP upload files
移动文件见How can I use FTP to move files between directories?
您可以将下载的文件直接流式传输到上传,以避免将文件存储到临时本地文件。
如果您想使用内置的 .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. What is not native .NET library though, but it 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 或更新版本。
我已经为此苦苦挣扎了一段时间,我真的不明白如何使用 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;
}
您的代码没有访问任何服务器。它不使用 FTP 或 SFTP。它适用于 local 文件。
无论如何,如果 FTP 和 SFTP 是您唯一可用的接口,则无法直接将文件从 FTP 复制到 SFTP 服务器。
您必须从FTP服务器下载文件,然后将它们上传到SFTP服务器。
用于在 FTP 服务器上查找最新文件:
FTP下载,见
SFTP上传见C# SFTP upload files
移动文件见How can I use FTP to move files between directories?
您可以将下载的文件直接流式传输到上传,以避免将文件存储到临时本地文件。
如果您想使用内置的 .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. What is not native .NET library though, but it 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 或更新版本。