EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Looking for SFTP Code Samples

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#4205
Posted: 11/02/2007 08:25:16
by Eugene Mayevski (EldoS Corp.)

Thank you. I will work with it immediately. I was trying to solve your problem on the component level (i.e. improve SFTP client to perform text conversion automatically) but there seems to be no reliable way to detect what OS the server runs on or what EOL convention it uses. So for now we can only help you with your sample, but not extend the component.


Sincerely yours
Eugene Mayevski
#4206
Posted: 11/02/2007 08:56:27
by Eugene Mayevski (EldoS Corp.)

Here we go ... This code piece writes the text files with Unix EOL markers (LF only).
The implementation is far from optimal, but it gives you the idea.

Code
...
Imports System.IO
Imports SBStringList
...

Sub SFTPFile()

        SBUtils.Unit.SetLicenseKey("03B0...87FC")

        Dim Handle() As Byte
        Dim DataBuffer() As Byte

        Dim CRBuf() As Byte
        ReDim CRBuf(0)
        CRBuf(0) = 10

        Client = New TElSimpleSFTPClient
        Client.Address = frmMain.TextBox3.Text
        Client.Port = "22"
        Client.Username = frmMain.TextBox1.Text
        Client.Password = frmMain.TextBox2.Text

        Dim InputStream As New FileStream(strLocalName, FileMode.Open)
        Dim list As New SBStringList.TElStringList()
        list.LoadFromStream(InputStream, System.Text.ASCIIEncoding.ASCII)

        Try
            Client.Open()
            Try
                ' opening the remote file
                Handle = Client.OpenFile(strRemoteName, SBSftpCommon.Unit.fmCreate Or SBSftpCommon.Unit.fmWrite Or SBSftpCommon.Unit.fmTruncate, Nothing)

                Dim curPos As Integer = 0

                Dim i As Integer
                For i = 0 To list.Count - 1
                    ' writing to the remote file
                    DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(list.Strings(i))
                    Client.Write(Handle, curPos, DataBuffer)
                    curPos = curPos + DataBuffer.Length
                    Client.Write(Handle, curPos, CRBuf)
                    curPos = curPos + 1
                Next i

                ' closing file handle once the upload has been finished
                Client.CloseHandle(Handle)

            Catch ex As Exception
                System.Console.WriteLine("Error during download: " + ex.Message, True)

            Finally
                Client.Close(False)
            End Try

        Catch ex As Exception
            System.Console.WriteLine(ex.Message)
        End Try

    End Sub


Sincerely yours
Eugene Mayevski
#4207
Posted: 11/02/2007 09:08:19
by Eugene Mayevski (EldoS Corp.)

Forgot to add one thing:


Code
Dim strRemoteName As String = "testfile.txt"
Dim AbsPath As String

...


Client.Open()
Try
    AbsPath = Client.RequestAbsolutePath(".")
    strRemoteName = AbsPath + "/" + strRemoteName
...


This code turns the relative path into the absolute one, as required by SFTP.


Sincerely yours
Eugene Mayevski
#4221
Posted: 11/05/2007 15:07:01
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Eugene thank you for all your help. However the upload time is taking over an hour for about 20,000 lines, so this method is not going to work.

I am going to have to modify the file on the server side in order to get the file modified to the format I need.

I have two more questions however.

1.) Permissions on transfer are set as 644, how do I change the permissions to 664? (rw-rw-r--)

2.) I want to do a private key transfer so I do not have to ask or code in a user password into the application. How would I do this? I know I use KeyStorage, but I am lost after that.
#4223
Posted: 11/06/2007 00:55:56
by Eugene Mayevski (EldoS Corp.)

As I mentioned, the implementation is not optimal. You must convert the file on the client side and send it in binary chunks.

Quote
James Galarneault wrote:
I am going to have to modify the file on the server side in order to get the file modified to the format I need.


Modification of the file on the server is not possible. You need to strip CR characters, making the file smaller. This would require that you read and write blocks of data. However this way you will have to transfer the data back and forth to/from the server.

Quote
James Galarneault wrote:
1.) Permissions on transfer are set as 644, how do I change the permissions to 664? (rw-rw-r--)


Use SetAttributes method. The knowledgebase on the site has instructions for how to do this.

Quote
James Galarneault wrote:
2.) I want to do a private key transfer so I do not have to ask or code in a user password into the application. How would I do this? I know I use KeyStorage, but I am lost after that.


Most of SFTP samples include public key authentication method. Please look there.


Sincerely yours
Eugene Mayevski
#4246
Posted: 11/07/2007 09:55:37
by James Galarneault (Standard support level)
Joined: 10/23/2007
Posts: 15

Actually what I am doing is transferring the file normally, and then I run a script on the server side using a command called "dos2ux" to convert the file, it will strip off the CR characters and output the file to another file. I was hoping to avoid doing that, but because of the speed of the code above I had to go that way.
#7836
Posted: 10/10/2008 07:39:18
by Guy Bowdler (Standard support level)
Joined: 10/10/2008
Posts: 1

Do we need to open the file before we upload it????

Abish
#7837
Posted: 10/10/2008 08:05:50
by Ken Ivanov (EldoS Corp.)

It depends on the exact method being used for upload. UploadFile() does not require you to open the file.
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 14413 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!