是否可以从 C# 同时将文件上传到同一服务器上的两个 SFTP 文件夹?

Is it possible to upload a file to two SFTP folders on same server at the same time from C#?

我正在为 SFTP 使用 SSH.NET 库。我需要将一个文件上传到同一台服务器上的两个文件夹中,但是文件会很大,所以我不想通过网络发送两次。

一旦数据在服务器上,是否有从一个文件夹复制到下一个文件夹的命令?或者可以同时上传而不必通过网络发送相同数据的两个副本?

您可以使用 Powershell FileSystemWatcher 之类的工具来监控您的某个文件夹中的更改。然后一旦有新文件添加到该文件夹​​,您可以触发一个操作(例如 Robocopy)并将文件复制到另一个文件夹。

您可能无法直接复制文件。具体原因见:
In an SFTP session is it possible to copy one remote file to another location on same remote SFTP server?

如果您有 shell 访问权限,您当然可以使用 shell 会话执行 cp 命令。
参见


通过 SFTP 复制远程文件的唯一可靠方法是下载并重新上传文件。

最简单的方法(无需创建临时本地文件)是:

SftpClient client = new SftpClient("example.com", "username", "password");
client.Connect();

using (Stream sourceStream = client.OpenRead("/source/path/file.dat"))
using (Stream destStream = client.Create("/dest/path/file.dat"))
{
    sourceStream.CopyTo(destStream);
}

但我知道这不是你想要的。