我们正在寻找一种方法来压缩数百个表并通过sftp将它们发送到另一台服务器。我们正在考虑使用SSIS,但如果没有某种类型的添加,SSIS将不会使用sftp。什么是另一种 - 可能更好 - 替代?
附加要求
我认为使用SSIS很难实现这两个要求。例如,需要批处理脚本来进行压缩。然后我们将有一个批处理脚本和一些c#代码来维护。也许另一种解决方案是按顺序..
假设性问题
这比拥有数百个SSIS包更高效(更快),每个SSIS包同时在不同的表上执行相同的SFTP传输吗? 想法是服务器将同时运行多个包,因为此方法一次只能运行一个转换,压缩和传输。
答案 0 :(得分:1)
这就是我使用SSIS实现这一目标的方法。我创建了一个可以在脚本任务中引用的C#DLL。我是这样做的,所以我也可以使用我的类库从其他应用程序SFTP。因此,如果您不想使用SSIS,您可以选择设置Windows服务。
首先我创建一个对WinSCPNet.dll的引用,在这里找到:
https://winscp.net/eng/docs/library
这是我创建的代码。它仍处于原型形式,你需要做一些事情,比如添加正确的错误处理/记录,我只使用Console.Writeline。
public class Sftp
{
public static int ListFiles(string Password, string HostName, string UserName, string SshHostKeyFingerprint)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = HostName,
UserName = UserName,
Password = Password,
SshHostKeyFingerprint = SshHostKeyFingerprint
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
RemoteDirectoryInfo directory = session.ListDirectory("/");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
public static int GetFiles(string Password, string HostName, string UserName, string SshHostKeyFingerprint)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = HostName,
UserName = UserName,
Password = Password,
SshHostKeyFingerprint = SshHostKeyFingerprint
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.GetFiles("/home/user/*", "d:\\download\\", true, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Download of {0} succeeded", transfer.FileName);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
// Copy and pasted code shared with PutFile, refactor if making changes.
public static int PutFiles(string Password, string HostName, string UserName, string SshHostKeyFingerprint, string SourceFolder, string RemoteFolder, string FileMask, string WinScpExePath)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = HostName,
UserName = UserName,
Password = Password,
SshHostKeyFingerprint = SshHostKeyFingerprint
};
using (Session session = new Session())
{
// Connect
session.ExecutablePath = WinScpExePath;
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(SourceFolder + FileMask, RemoteFolder, false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
return 1;
}
}
// Copy and pasted code from PutFiles, refactor if making changes.
public static int PutFile(string Password, string HostName, string UserName, string SshHostKeyFingerprint, string InputFile, string RemoteFolder, string WinScpExePath)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = HostName,
UserName = UserName,
Password = Password,
SshHostKeyFingerprint = SshHostKeyFingerprint
};
using (Session session = new Session())
{
// Connect
session.ExecutablePath = WinScpExePath;
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(InputFile, RemoteFolder, false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
return 1;
}
}
}