Solved

Creating a file in the FTP Folder using SFTP

Posted on 2014-09-12
6
593 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

With the withdrawal of support for Windows Server 2003 this summer, many clients face the issue of moving away from their 2003 installs. There are a few options out there that many people/companies are selling. But the clients I have, haven't wanted…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
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, f…

776 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