尝试使用SSIS脚本任务(C#)通过SFTP获取zip文件

时间:2019-06-24 10:17:23

标签: c# ssis sftp winscp

我正在尝试连接到winscp,并使用以下代码作为参考来从SFTP服务器获取zip文件。我添加了WinSCPnet.dll程序集的参考。 我要下载ABC_EMP.txt.zip,该文件位于Winscp文件夹my_data中。

示例代码:

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

namespace ST_5a30686e70c04c5a8a93729fd90b8c79.csproj
{

    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,
                PortNumber = 22,
                SshHostKeyFingerprint = (string) Dts.Variables["User::SshHostKeyFingerprint"].Value
            };

            try
            {
                using (Session session = new Session())
                {
                     session.SessionLogPath="G:/Testing/SFTP_Log_Details/winscp.log";
                    // 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.GetFiles("/my_data/ABC_EMP.txt.zip",@"G:\TestFolder\Download\");

                    // 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;
            }
        }
    }
}

我已生成“ SshHostKeyFingerprint”为“ ssh-rsa 2048 9b:63:5e:c4:26:bb:35:0d:49:e6:74:5e:5a:48:c0:8a”

enter image description here

脚本任务被执行并失败,并显示以下错误代码“调用目标已抛出异常”。日志文件夹也为空,未写入任何日志。

0 个答案:

没有答案