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

问题描述 投票:3回答:2

请,我需要帮助只将当前目录中的当前日文件加载到SFTP服务器。显然,SSIS中的FTP任务无法移动到SFTP,只能移动到FTP。

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

ssis sftp filezilla
2个回答
4
投票

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

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

使用WinSCP可以轻松完成任务,因为它具有选择今天文件的语法。

您可以使用批处理文件,如:

winscp.com /ini=nul /command ^
    "open sftp://username:password;[email protected]/" ^
    "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,包括host key fingerprint part

参考文献:

你可以use the script in SSISschedule it with Windows scheduler

(我是WinSCP的作者)


0
投票

您可以使用Winscp的SSIS脚本任务在Winscp上使用ssis包的调度作业在FTP上传文件

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

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;
© www.soinside.com 2019 - 2024. All rights reserved.