Upload to FTP via a Memory Stream without creating any files

Advertisement

Pavoyas
Joined:
Posts:
2
Location:
UK

Upload to FTP via a Memory Stream without creating any files

Although the Session.PutFile method allows uploading a file from a stream, it seems that it creates a file behind the scenes.

It is an unavoidable and key requirement for me that no files are created on the file system when uploading to the FTP/SFTP server. The contents that I'm uploading are generated on the fly, and must not be stored on disk.

Is this at all possible using WinSCP?

Sample code:
$stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($queryFile.content))
$transfer = New-WinSCPTransferOption -OverwriteMode Overwrite
 
$session.PutFile($stream, "./somedirectory/$($queryFile.fileName).csv", $transfer)
The file is uploaded successfully, but I can see that the .csv file is created on the machine after running this code.

Please note that simply deleting the file after-the-fact isn't an acceptable solution. The file must not be saved locally at all.

Thanks in advance for any assistance you can provide.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,506
Location:
Prague, Czechia

Re: Upload to FTP via a Memory Stream without creating any files

The Session.PutFile does not create any file. Not even temporarily, let only permanently.

If you have a different experience, please post a complete example and log files.

Also note that New-WinSCPTransferOption comes from a 3rd party module that we have nothing to do with.

Reply with quote

Pavoyas
Joined:
Posts:
2
Location:
UK

Thank you for your response, Martin. That's very odd in that case, as I get the following exception:
Error transferring file 'nameofthefile.csv'.
Copying files to remote side failed.
The process cannot access the file because it is being used by another process.
As far as my PowerShell script goes – there's absolutely nowhere where that file could be created. The only place where the $($queryFile.fileName) is used is in the remote path parameter of PutFile as shown in the original post.

Could it be red herring? Is it maybe talking about the remote file being locked instead?

Thank you.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,506
Location:
Prague, Czechia

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum