Ligação à SFTP via SSIS
estou a tentar ligar-me a um servidor SFTP
através de um pacote SSIS
. O pacote executa WinSCP
com o seguinte texto de ligação num ficheiro .txt
:
open sftp://username:fc$#[email protected]:22
No entanto, o pacote continua a falhar sem ser capaz de se ligar. Tem alguma coisa a ver com os caracteres especiais da senha?
Eu sou capaz de me conectar a um SFTP diferente se eu substituir a string então eu sei que deve ser algo a ver com a sintaxe acima. Eu tentei colocar aspas duplas em torno do texto como segue sem sucesso:
open "sftp://username:fc$#[email protected]:22"
2
Author: Martin Prikryl, 2015-12-22
1 answers
Tive de o fazer também, recentemente, para um dos meus projectos de trabalho. Nós usamos o WinSCP. net assembly dentro de uma tarefa de Scripting SSIS, pois isto é o que WinSCP também recomenda como a maneira de alcançar SFTP usando WinSCP em SSIS.
Veja este guia - usando a montagem WinSCP.NET do SQL Server Integration Services (SSIS). Ele o acompanha através da instalação e configuração e também contém o código de exemplo de trabalho (depois de mudar o script para suas necessidades, é claro!).
Amostra código-depois de referenciar o conjunto WinSCPnet.dll
- está abaixo.
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;
}
}
}
}
2
Author: Shiva, 2015-12-23 07:18:07