【问题标题】:Transfer of large number of files using SFTP is slow in C#在 C# 中使用 SFTP 传输大量文件很慢
【发布时间】:2016-05-27 11:46:22
【问题描述】:

我正在使用 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));
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# multithreading sftp winscp winscp-net


    【解决方案1】:

    每个文件都有相当大的开销(打开、关闭、更新时间戳)。所以大量小文件的传输效率很低。

    你可以做的是并行传输。

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

    看这个例子:
    Automating download in parallel connections over SFTP/FTP protocol

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 2014-08-18
      • 2015-04-02
      • 2014-12-29
      • 2013-02-13
      • 2015-08-04
      相关资源
      最近更新 更多