[SFTP] Private key

[SFTP] Private key

  
Forge Component
(3)
Published on 24 Sep by Wei Zhu
3 votes
Published on 24 Sep by Wei Zhu
Hello,

Is there also a possiblilty to use a private key to connect to a sftp environment?

Greetz
Hi,

Not in this version, but you can open the extension (.xif) and then customize the .NET code in Visual Studio.
Thnx Paulo,

Is there maybe a way to update this script that in case a folder does not exist it will create it?
Maybe this script can be updated:

// Upload the temp file
var sftp = new Renci.SshNet.SftpClient(ssIP, ssPort, ssUsername, ssPassword);
sftp.Connect();
var file = new FileStream(localFile, FileMode.Open);
sftp.UploadFile(file, ssPath);  // Optional: canOverride
file.Close();
sftp.Disconnect();

Greetz!

Hi,

Probably it can be done.
The component is based in an underlying library - SSH.NET, hosted here: sshnet.codeplex.com. Maye you can find a sample or documentation for your scenario. Btw, did you try using Put with the non-existing subfolder? (I'm not sure if it will simply throw an error - most likely - or actually create it.)
I changed your code to the following:

// Upload the temp file
            var sftp = new Renci.SshNet.SftpClient(ssIP, ssPort, ssUsername, ssPassword);
            sftp.Connect();
            if (!sftp.Exists(ssPath))
            {
                sftp.CreateDirectory(ssPath);
            }

            if (sftp.Exists(ssPath + ssFilename))
            {
                ssExists = true;
            }
            else
            {
                var file = new FileStream(localFile, FileMode.Open);
                sftp.UploadFile(file, ssPath + ssFilename);  // Optional: canOverride
                file.Close();
                sftp.Disconnect();
            }

I took the ssPath to ssPath and ssFilename so you can check if the path exists. And I added a output boolean to tell if the file already exist.
Is there also a way to detect the mime-type of the file, when downloading it?
You can try this component.