EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Looking for SFTP Code Samples

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#4178
Posted: 10/30/2007 10:02:43
by Eugene Mayevski (EldoS Corp.)

What you are asking for is that we create a sample for you. This service is available for users with Standard support level (i.e. the customers who have purchased a license).


Sincerely yours
Eugene Mayevski
#4188
Posted: 10/31/2007 11:18:19
by Ken Ivanov (EldoS Corp.)

Please use the following code as a guide:
Code
' buffer with data to be uploaded
Dim DataBuffer() as Byte

' SFTP file handle variable
Dim Handle() As Byte

' opening the remote file
Handle = cli.OpenFile("RemoteFile", SBSftpCommon.Unit.fmCreate Or SBSftpCommon.Unit.fmWrite Or SBSftpCommon.Unit.fmTruncate, Nothing)

' writing to the remote file
cli.Write(Handle, 0, DataBuffer)

' closing file handle once the upload has been finished
cli.CloseHandle(Handle)
#4193
Posted: 10/31/2007 14:29:12
by Eugene Mayevski (EldoS Corp.)

"RemoteFile" should be the absolute path to the file, eg. /path/to/filename.ext


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

Thank you for the code, I have tried it out and am having the following issue now:

When it tries to open the remote file (the one that exists on the windows location), it says no such file exists.

Do I need to upload the file first to the Unix server, then open it and write it then?
#4197
Posted: 11/01/2007 11:59:46
by Eugene Mayevski (EldoS Corp.)

There are several reasons possible:
1) the remote path is not correct for the server. It can be valid for you (when you browse the directory listing), but not for the server which builds the folder hierarchy. You can use RequestAbsolutePath method right after login to find out the location of your home folder. Then append the desired path to the path provided by the above method.
2) Try removing fmTrunc­ate from the parameters. Some servers might interpret the parameters wrong when both Create and Truncate are specified.


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

Ok let me take a step back.

I have a VB.NET application that processes a file and extracts data into another text file.

The text file that was just created I want to transfer from a Windows Server to a Unix server. Unfortunately the regular SFTP transfer adds extra characters at the end of each line and the Unix Server is at protocol 3 so I can't use the Ascii functions you have.

What I need to do is transfer the new file on the Windows server to the Unix Server using the functions you listed above.

I have the code above working, but when I try to upload the file first and then open it, the file is empty.

The code snippet I have is this, I know I am missing something obvious here..... Oh I am using .NET 2005 SDK 2.0

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

Client = New TElSimpleSFTPClient
'Client.KeyStorage = KeyStorage
Client.Address = "1.1.1.1"
Client.Port = "22"
Client.Username = "something"
Client.Password = "something else"
strRemoteName = "/unix server location/" & strFileName
strLocalName = filMiss

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

' writing to the remote file
Client.Write(Handle, 0, DataBuffer)

' 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

#4199
Posted: 11/01/2007 13:26:55
by Eugene Mayevski (EldoS Corp.)

Frankly speaking, I am a bit confused with your last message. Let's ignore the "upload the file first and then open it". Looking at your code, it's not clear whether you actually send the data. DataBuffer is empty in the above code piece, so the above code, if executed, will produce a zero-byte-long file on the server. Please post a piece of your actual code, or, even better, try to create a simple test project which performs the function you need. Then we will be able to look at it and you will be able to easily debug this sample project and once it's working copy the function from the test to your real project.


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

Eugene I made a test application, I know one of the errors is that I do not have the databuffer set to any value.

Pasted below is the code I set up:

For frmMain:

Public Class frmMain


Private Sub btnSFTPFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSFTPFile.Click
Call SFTPFile()
MsgBox("File Loaded")
End Sub


End Class

frmMain has one button to SFTP the file, and three text boxes to set the User ID, the Password, and Server IP address.


for modSFTP:

Imports SBSftpCommon
Imports SBSimpleSftp

Module modSFTP

Dim Client As TElSimpleSFTPClient
Dim strRemoteName As String = "/"
Dim strLocalName As String = "c:\temp\testfile.txt"
Dim strFileName As String = "testfile.txt"

Sub SFTPFile()

SBUtils.Unit.SetLicenseKey("0190...773A")
Dim Handle() As Byte
Dim DataBuffer() As Byte

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

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

' writing to the remote file
Client.Write(Handle, 0, DataBuffer)

' 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

End Module


Attached is the testfile I am trying to open and write to the Unix Server.

The file name is "testfile.txt" (can place in c:\temp folder)

testfile.txt contains data similar to what I am trying to upload from the C drive to the Unix Server.

Each line contains 11 characters and there can be anywhere from 1 to 30,000+ lines transferred.



[ Download ]
#4201
Posted: 11/01/2007 15:25:38
by Eugene Mayevski (EldoS Corp.)

Understood :(. I will create a sample for you tomorrow.


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

Eugene my brain was turned off yesterday. I realized I could have zipped the application when I got home last night.

Here is the complete package along with the sample file.


[ Download ]
Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.

Reply

Statistics

Topic viewed 14401 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!