Search code examples
.netvb.netsftpwinscpwinscp-net

WinSCP upload file to remote SFTP error: "Cannot create remote file – Permission denied"


We were doing some manual process to download from and upload to a remote SFTP server with FileZilla. With the client software we didn't have any permission issues.

Recently we decided to move it to a scheduled function using VB.NET. The downloading worked well (so I removed it from my code just to make the code sample concise).

But for uploading, the program ran into an error:

WinSCP.SessionRemoteException: 'Cannot create remote file '/some path/on/remote/myFile.txt.filepart'.

Permission denied.

Error code: 3

Error message from server (en): Permission denied'

Below is the code for uploading the file.

Using session As New Session
    session.Open(sessionOptions)

    Dim transferOptions As New TransferOptions
    transferOptions.TransferMode = TransferMode.Binary

    Dim transferResult As TransferOperationResult
    
    ' localFilePath = "C:\somepath\myFile.txt"
    If Not String.IsNullOrEmpty(localFilePath) And File.Exists(localFilePath) Then
        transferResult = session.PutFiles(localFilePath, "/some path/on/remote/", False, transferOptions)     
        transferResult.Check()   'error was thrown here
    Else
        Throw New FileNotFoundException("The file could not be found")
    End If
End Using

Any help is appreciated, thank you for your time.


Solution

  • With SFTP protocol, WinSCP by default transfers files over 100 KB via a temporary file. That won't work if you do not have permissions to create a new file.

    In such case, you will need to disable the transfer via a temporary file (aka resumable transfer). For that set TransferOptions.ResumeSupport:

    Dim transferOptions As New TransferOptions
    transferOptions.ResumeSupport.State = TransferResumeSupportState.Off
    
    transferResult =
        session.PutFiles(localFilePath, "/remote/path/", False, transferOptions)
    transferResult.Check()