将今天的文件从本地目录上传到SFTP服务器

Upload today's files from local directory to SFTP server

拜托,我需要帮助仅将当前日期的文件从本地目录加载到 SFTP 服务器。显然,FTP SSIS 中的任务无法移动到 SFTP,只能移动到 FTP.

另外,我有FileZilla。我可以在 SSIS 中使用 FileZilla 吗?或者我可以让 FileZilla 在特定时间自动发送文件吗? (使用 Windows 10)

您不能使用 FileZilla。 FileZilla 不支持任何类型的脚本。

还有许多其他可编写脚本的 SFTP 客户端。

使用 WinSCP,您的任务很简单,因为它具有 select 今天的文件的语法。

您可以使用像这样的批处理文件:

winscp.com /ini=nul /command ^
    "open sftp://username:password;fingerprint=hostkeyfingerprint@example.com/" ^
    "put -filemask=*>=today ""c:\local\path\*"" ""/remote/path/""" ^
    "exit"

>=today keyword 仅受 WinSCP 5.15 及更新版本支持。

在旧版本中,您可以使用 %TIMESTAMP% syntax,特别是 >=%%TIMESTAMP#yyyy-mm-dd%%,而不是 >=today

你可以WinSCP GUI generate the batch file template for you, including the host key fingerprint part.

参考文献:

你可以use the script in SSIS or schedule it with Windows scheduler.

(我是WinSCP的作者)

您可以使用 SSIS 脚本任务和 Winscp 在 FTP 上使用 Winscp 借助 ssis 包

的调度作业上传文件

在脚本任务中使用以下代码

string winscpPath = Dts.Variables["winSCPPath"].Value.ToString(); 
string username = Dts.Variables["ftpUsername"].Value.ToString(); 
string password = Dts.Variables["ftpPassword"].Value.ToString(); 
string ftpSite = Dts.Variables["ftpSite"].Value.ToString(); 
string localPath = Dts.Variables["localPath"].Value.ToString(); 
string remoteFTPDirectory = Dts.Variables["remoteFTPDirectory "].Value.ToString(); 
string sshKey = Dts.Variables["sshKey"].Value.ToString();
Boolean winSCPLog = (Boolean)Dts.Variables["winSCPLog"].Value;
string winSCPLogPath = Dts.Variables["winSCPLogPath"].Value.ToString();

SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = ftpSite,
UserName = username,
Password = password,
SshHostKeyFingerprint = sshKey
};

try
{
  using (Session session = new Session())
  {
    // WinSCP .NET assembly must be in GAC to be used with SSIS,
    // set path to WinSCP.exe explicitly, if using non-default path.
    session.ExecutablePath = winscpPath;
    session.DisableVersionCheck = true;

    if(winSCPLog)
    {
      session.SessionLogPath = @winSCPLogPath + @"WinscpSessionLog.txt";
      session.DebugLogPath = @winSCPLogPath + @"WinscpDebugLog.txt";
    }

    // Connect
    session.Timeout = new TimeSpan(0,2,0); // two minutes
    session.Open(sessionOptions);

    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    try
    {
      session.GetFiles(remoteFTPDirectory + "/" + 
      fileToDownload, localPath, false, transferOptions);
    }
    catch (Exception e)
    {
      Dts.Events.FireError(0, null,
      string.Format("Error when using WinSCP to download file: {0}", e), null, 0);
      Dts.TaskResult = (int)DTSExecResult.Failure;
    }
  }
}
Dts.TaskResult = (int)ScriptResults.Success;