通过 SSIS 连接到 SFTP

Connecting to SFTP via SSIS

我正在尝试通过 SSIS 程序包连接到 SFTP 服务器。该包在 .txt 文件中使用以下连接字符串执行 WinSCP

open sftp://username:fc$#6444@example.com:22

然而,包裹总是失败,无法连接。与密码中的特殊字符有关吗?

如果我替换字符串,我可以连接到不同的 SFTP,所以我知道它一定与上面的语法有关。我试过在字符串周围加上双引号,但没有成功:

open "sftp://username:fc$#6444@example.com:22"

我最近也不得不这样做,因为我的一个工作项目。我们在 SSIS 脚本任务中使用了 WinSCP .NET 程序集,因为这也是 WinSCP 推荐的在 SSIS 中使用 WinSCP 实现 SFTP 的方法。

请参阅本指南 - Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS)。它会引导您完成安装和设置,还包含工作示例代码(当然是在您根据需要更改脚本之后!)。

示例代码 - 在您引用 WinSCPnet.dll 程序集后 - 如下。

using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
using System.AddIn;
using WinSCP;

namespace ST_5a30686e70c04c5a8a93729fd90b8c79.csproj
{
    [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                // To setup these variables, go to SSIS > Variables.
                // To make them accessible from the script task, in the context menu of the task,
                // choose Edit. On the Script task editor on Script page, select ReadOnlyVariables,
                // and tick the below properties.
                HostName = (string) Dts.Variables["User::HostName"].Value,
                UserName = (string) Dts.Variables["User::UserName"].Value,
                Password = (string) Dts.Variables["User::Password"].Value,
                SshHostKeyFingerprint = (string) Dts.Variables["User::SshHostKeyFingerprint"].Value
            };

            try
            {
                using (Session session = new Session())
                {
                    // As WinSCP .NET assembly has to be stored in GAC to be used with SSIS,
                    // you need to set path to WinSCP.exe explicitly, if using non-default location.
                    session.ExecutablePath = @"C:\winscp\winscp.exe";

                    // Connect
                    session.Open(sessionOptions);

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    bool fireAgain = false;
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Dts.Events.FireInformation(0, null, 
                            string.Format("Upload of {0} succeeded", transfer.FileName),
                            null, 0, ref fireAgain);
                    }
                }

                Dts.TaskResult = (int)DTSExecResult.Success;
            }
            catch (Exception e)
            {
                Dts.Events.FireError(0, null,
                    string.Format("Error when using WinSCP to upload files: {0}", e),
                    null, 0);

                Dts.TaskResult = (int)DTSExecResult.Failure;
            }
        }
    }
}

安装 WinSCP 然后创建一个文件夹,您需要从客户端获取文件或将 file.Then 打开执行进程任务然后转到 Expression 选项卡并使用以下代码设置 ExecutableArguments(请相应更改)。 在记事本中写下这段代码,并在路径 C:\path\to\winscp.txt.

中另存为 winscp.txt

打开sftp://Host_Name:密码@apacsftp01.mftservice.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx...="

get -delete /home/client/Share/MediaData/Media_file.xlsx

退出