This project is read-only.

SFTP Permission Denied Exception

Mar 20, 2012 at 12:24 PM
Edited Mar 20, 2012 at 12:40 PM

Hello, i'm trying to loop through a designated directory and upload all files within it to a server using SFTP:

The exception "Permission Denied" is being thrown  on the SFTP.UploadFile line, and im not entirely sure why, the Uploads folder that is being used has all read and write permissions set and ive been able to upload this file using both WinSCP and the WinSCP assembly from VB.Net . Any help with this issue would be greatly appreciated.

Thanks

Paul

Here is my code:

 
        Dim SFTP As Renci.SshNet.SftpClient
        Dim Directory As New DirectoryInfo(FolderName)
        Dim FileArray As FileInfo() = Directory.GetFiles()
        Dim File As FileInfo
        Dim FileName As String
        Dim UploadFile As System.IO.FileStream


        'Get date and time
        Dim DateAndTime As String = DateTime.Now.ToString("dd-MM-yyyy-hhmmss")
        Dim UploadDirectory As String
        
        'Hostname, uName and pWord purposefully removed.
        'Set host name
        Dim HostName As String = "Host"
        'Set user name
        Dim UserName As String = "uName"
        'Set password
        Dim Password As String = "pWord"

        'Create new SFTP Object
        SFTP = New Renci.SshNet.SftpClient(HostName, UserName, Password)

        SFTP.Connect()

        'Create uploaded folder name
        UploadDirectory = "Uploaded" & "-" & DateAndTime & "-" & UsernameGlobal
        SFTP.ChangeDirectory("Uploads")
        'Create Directory using folder name
        SFTP.CreateDirectory(UploadDirectory)


        'Loop for each file in desktop directory
        For Each File In FileArray
            FileName = FolderName & "/" & File.Name
            UploadFile = New System.IO.FileStream(FileName, FileMode.Append)
            SFTP.UploadFile(UploadFile, UploadDirectory)
        Next

       'Close connections/file streams
        SFTP.Disconnect()
        UploadFile.Close()
        UploadFile.Dispose()
        SFTP.Dispose()

Mar 20, 2012 at 1:29 PM

Hi,

 

Sorry that I cant be any more helpfull but "Permission Denied" error that you get is returned by SFTP subsystem from the server so there is no way for me to know what exactly wrong.

 

I guess is that any other library that you would use you will get the same error.

The only thing I can suggest is to look again into folder permissions on the server.

 

Hope it helps.

 

Thanks,

Oleg

Mar 20, 2012 at 1:47 PM

Hello Again!

Thanks for your speedy reply!

Turns out i was using the wrong path in the SFTP.UploadFile method, i thought this was the path where the files were to be uploaded but its actually for naming the files once uploaded.  I've included my complete code for anyone who may have the same issue.  Thanks for your help.

Paul

Code:

        Dim SFTP As Renci.SshNet.SftpClient
        Dim Directory As New DirectoryInfo(FolderName)
        Dim FileArray As FileInfo() = Directory.GetFiles()
        Dim File As FileInfo
        Dim FileName As String
        Dim UploadFile As System.IO.FileStream


        'Get date and time
        Dim DateAndTime As String = DateTime.Now.ToString("dd-MM-yyyy-hhmmss")
        Dim UploadDirectory As String
        
        'Host, uName, pWord Removed Intentionally
        'Set host name
        Dim HostName As String = "HostName"
        'Set user name
        Dim UserName As String = "uName"
        'Set password
        Dim Password As String = "pWord"

        Try

            'Create new SFTP Object
            SFTP = New Renci.SshNet.SftpClient(HostName, UserName, Password)

            SFTP.Connect()

            'Create uploaded folder name
            UploadDirectory = "Uploaded" & "-" & DateAndTime & "-" & UsernameGlobal
            SFTP.ChangeDirectory("Uploads")
            'Create Directory using folder name
            SFTP.CreateDirectory(UploadDirectory)
            SFTP.ChangeDirectory(UploadDirectory)


            'Loop for each file in desktop directory
            For Each File In FileArray
                FileName = FolderName & "/" & File.Name
                UploadFile = New System.IO.FileStream(FileName, FileMode.Open, FileAccess.ReadWrite)
                SFTP.UploadFile(UploadFile, File.Name)
            Next

            'Close connections / file streams
            SFTP.Disconnect()
            UploadFile.Close()
            UploadFile.Dispose()
            SFTP.Dispose()

    End Sub