WinSCP: How to make sure SFTP upload gets renamed from .zip.filepart to .zip?
Asked Answered
M

4

6

Using the .NET assembly of WinSCP to upload a file. OperationResultBase.Check() is throwing the following error:

WinSCP.SessionRemoteException: Transfer was successfully finished, but temporary transfer file 'testfile.zip.filepart' could not be renamed to target file name 'testfile.zip'. If the problem persists, you may want to turn off transfer resume support.

It seems that this happens with any zip file that I try to send. If it makes a difference, these are zip files that were created using the DotNetZip library.

Code that I'm using, taken pretty much directly from the example in the WinSCP documentation:

public void uploadFile(string filePath, string remotePath)
{
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;
    TransferOperationResult transferResult;
    transferResult = currentSession.PutFiles(filePath, remotePath, false, transferOptions);
    transferResult.Check();
    foreach (TransferEventArgs transfer in transferResult.Transfers)
    {
        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
    }
}

Discussion over at the WinSCP forum indicates that the assembly doesn't yet allow programmatic control of transfer resume support. Is there a workaround for this?

Mobility answered 1/6, 2012 at 22:34 Comment(6)
It seems odd to me that the upload is using a different filename than the filename actually desired. Do you have control over that?Discernible
@sarnold, from the WinSCP documentation: "When transferring file with SFTP protocol, it is first transferred into a temporary file with .filepart extension. Only after the transfer is completed the extension is removed."Mobility
If you have access to the server, is it possible to run something like Process monitor from SysInternals to see what happens when the file upload finishes. For example, an anti-virus program might start to scan the file the moment the file is closed and still be accessing the file when the rename is attempted.Tingle
@sgmoore, unfortunately I don't have server access.Mobility
There's a typo in the bounty text. Instead of ".NET assembly of C#", it should say ".NET assembly of WinSCP". -- from sigil. Sorry man, can't edit it without removing it.Sidereal
Have you tried putting a short delay between the upload and the check? It might just be that the file system is not responding fast enough.Casta
P
4

It sounds as if the filesystem on the destination server where the file is getting uploaded to does not allow file change permissions. This could be causing the renaming of the file at the finish of the upload to fail despite the fact that the complete file was uploaded and written to the filesystem with the temporary file name used while the transfer was in progress. If you don't have administrative access to the destination server, you can test that by trying to rename a file that is already on the destination server. If that fails also, then you will either need to have the proper permissions on the destination server changed in order for that to work. Otherwise you might have to use the advice provided in your error message to turn off the resume support so it is initially opened for writing with the desired filename instead of the temporary filename (with the .filepart extension).

Parquetry answered 1/6, 2012 at 22:40 Comment(3)
@Discernible - You are correct, I kept referring to FTP in my response instead of SCP. I was downloading some log files via FTP while I was typing, thus the error in nomenclature. Will fix, as the response is otherwise as intended.Parquetry
I can manually change the filename using a GUI session of WinSCP. As I noted at the end of my question, the .NET assembly doesn't have a function for controlling the transfer resume support. WinSCP is open source, so I can write a C# wrapper for the function I want, but I was hoping for a faster solution.Mobility
Another quick note on this: I have permission to change filenames on the destination server. Also, the upload process works fine when I do it using the GUI; it's only when I try to do it through .NET that it fails.Mobility
S
2

Turn off the resumesupport:

put *.txt -nopreservetime -nopermissions -resumesupport=off
Shir answered 8/4, 2015 at 2:5 Comment(0)
W
1

It would help, if you included full error message, including root cause as returned by the server.

My guess is that there's an antivirus application (or similar) running on the server-side. The antivirus application checks any file once upload finishes. That conflicts with WinSCP attempt to rename the file once the upload is finished. The problem may tend to occur more frequently for .ZIP archives, either because they tend to be larger or simply because they need to get extracted before the check (what takes time).

Anyway, you can disable the transfer to temporary file name using the TransferOptions.ResumeSupport.

See also the documentation for the error message "Transfer was successfully finished, but temporary transfer file ... could not be renamed to target file name ..."

Whipping answered 22/4, 2014 at 15:2 Comment(0)
J
0

All you have to do is to disable TransferResumeSupport using the below code.

TransferOptions transferOptions = new TransferOptions();

transferOptions.ResumeSupport = new TransferResumeSupport {State = TransferResumeSupportState.Off };
Jeana answered 2/12, 2022 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.