Solved

Creating a file in the FTP Folder using SFTP

Posted on 2014-09-12
6
559 Views
Last Modified: 2014-09-15
Using third party tool Chilkat, i am creating a csv file in the server. file transfer is taking place through sftp. I have mentioned in the uri link : ftp://home/folder/folder/subfolder/, to create the csv file.

Apparently, csv file is created in the root directory. ftp://home/user_id/

We expect the file to be created in the mentioned file folder. How should i fix this issue ?

Sample source code : http://www.example-code.com/vbnet/sftp_uploadFile.asp

Public Function UploadFileFromStream(ByVal data As Byte(), ByVal _fileName As String) As Boolean

        Try

            Dim sftp As New Chilkat.SFtp()
            Dim success As Boolean
            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            success = sftp.Connect(_uri, _port)

            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication

            success = sftp.AuthenticatePw(_userId, _password)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp()
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

          
            Dim handle As String
            handle = sftp.OpenFile(_fileName, "writeOnly", "createTruncate")
            If (handle = vbNullString) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            success = sftp.WriteFileBytes(handle, data)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If
            'sftp.Disconnect()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

        Return True

    End Function

Open in new window

0
Comment
Question by:chokka
  • 4
  • 2
6 Comments
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 40322871
according to the following link

     http://www.example-code.com/vbdotnet/sftp_realPath.asp

all files accessable by Chilkat.SFtp are below user's home folder.

if I understand the remarks correctly you either need to give a relative path like ./folder/folder/subfolder/xxx.csv or an absolute path which is not an ftp url but the absolute path after user login.

Sara
0
 

Author Comment

by:chokka
ID: 40323135
@sara, thank you !!  I am very new to sftp.

Okay, I am willing to give either relative path or absolute path which you suggested.

Issue is in FTP, we can access the folder only by a link which starts with ftp://computername/folder/file.csv.   And to access this link, we have a username / password.



Whether the rule is same for sftp ?? How to set the relative or absolute path for sftp ?
0
 

Author Comment

by:chokka
ID: 40323591
@sara, another question. How to give a relative or absolute file path in vb.net for sftp.

''' <summary>
    '''  Passing variable data and filename as Input parameters.
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="_fileName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UploadFileFromStream(ByVal data As Byte(), ByVal _fileName As String) As Boolean

        Try

            Dim sftp As New Chilkat.SFtp()
            Dim success As Boolean
            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            ' uri is the filepath - For now : ftp://home/foldername/subfoldername/file.csv
            ' uri expected to take sftp folder path : /home/subfoldername/file.csv
            'Port number always 22

            success = sftp.Connect(_uri, _port)

            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication

            success = sftp.AuthenticatePw(_userId, _password)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp()
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

          
            Dim handle As String
            handle = sftp.OpenFile(_fileName, "writeOnly", "createTruncate")
            If (handle = vbNullString) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            ' Variable : byte is written as .csv file 
            success = sftp.WriteFileBytes(handle, data)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If
            'sftp.Disconnect()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

        Return True

    End Function

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Closing Comment

by:chokka
ID: 40323821
Thanks for sharing your answer.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40324170
did you succeed with relative path?

Sara
0
 

Author Comment

by:chokka
ID: 40324247
@sara, Not really. You gave a tip about Absolute file path and relative file path.

Again , i am using a third party dll. Name of the dll is Chilkat sftp. I am not sure whether the chilkatsftp supports Absolute file path or relative file path.

One of their sample program:
http://www.cknotes.com/understanding-sftp-absolute-filepaths/

Right now, my file is copied in
/home/<user_id>

Open in new window


I have posted new question for file copy method in linux. I am planning to take it as a batch file which can be executed from .Net Program
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now