通过SSIS连接到SFTP

时间:2015-12-22 00:29:14

标签: ssis sftp winscp ssis-2012

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

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

但是,无法连接,程序包仍会失败。是否与密码中的特殊字符有关?

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

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

2 个答案:

答案 0 :(得分:3)

对于我最近的一个工作项目,我也必须这样做。我们在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;
            }
        }
    }
}

答案 1 :(得分:1)

安装 WinSCP ,然后在要从客户端获取文件或放置文件的位置创建一个文件夹。然后打开执行过程任务,然后转到 Expression 标签并进行设置可执行文件参数以及以下代码(请相应更改)。 将此代码写在记事本中,并另存为winscp.txt,位于路径C:\ path \ to \ winscp.txt。

打开sftp:// Host_Name:Password@apacsftp01.mftservice.com/ -hostkey =“ ssh-rsa 2048 xxxxxxxxxxx ... =”

获取-delete /home/client/Share/MediaData/Media_file.xlsx

enter image description here

退出