繁体   English   中英

在 C# 中使用 SFTP 传输大量文件很慢

[英]Transfer of large number of files using SFTP is slow in C#

我正在使用 C# WPF 应用程序中的 SFTP 在 Linux 服务器上上传 4000 个大小为 85 KB 的 zip 文件。 整个过程需要 30 分钟。

有什么办法可以加快使用 SFTP 的上传速度?

我正在使用 WinSCP .NET 程序集:
https://winscp.net/eng/docs/library

我以前也用过 Chilkat。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinSCP;
namespace SFTP_Demo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string line;
            SessionOptions sessionoptions = new SessionOptions()
            {
                Protocol = WinSCP.Protocol.Sftp,
                HostName = "172.168.1.7",
                PortNumber = 22,
                UserName = "lduser",
                Password = "lduser",
                GiveUpSecurityAndAcceptAnySshHostKey = true
            };
            using (Session session = new Session())
            {
                session.Open(sessionoptions);
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
                TransferOperationResult transferResult;
                System.IO.StreamReader file = new System.IO.StreamReader(txtFile.Text);
                while ((line = file.ReadLine()) != null)
                {
                    transferResult = session.PutFiles(@"D:\Test\signature\ldoutput\"+line, "/SFTP/", false, transferOptions);
                    transferResult.Check();
                    counter++;
                    strbldr = strbldr.AppendLine(string.Format("{0} Upload of {1} succeeded", counter + 1.ToString(), line));
                }
            }
        }
    }
}

每个文件都有相当大的开销(打开、关闭、更新时间戳)。 因此,传输大量小文件的效率非常低。

您可以做的是并行传输。

使用Session.ListDirectory (或Session.EnumerateRemoteFiles如果您需要递归)收集文件列表并将列表拆分为批次,在单独的线程中传输每个文件。

看这个例子:
通过 SFTP/FTP 协议在并行连接中自动下载

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM