为什么我的ftp上传方法如此缓慢? [英] Why is my ftp upload method so slow?

查看:233
本文介绍了为什么我的ftp上传方法如此缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个控制台应用程序来从FTP下载文件,然后上传到不同的FTP位置。
文件下载大约需要10秒,但上传时间大约需要6分钟。
每个文件大小为5-30KB,共有256个文件。非常小。

上传和下载代码非常相似,它遍历目录中的所有文件,然后上传。它很简单,如下所示,它从D:\LEV\文件夹迭代并上传文件到ftp。

编辑:这是在Azure上运行的'小'的Windows虚拟机,所以我假设带宽不是问题?
另外我在另一台使用windows ftp.exe上传的虚拟机上执行相同的任务,比同一台机器上的控制台应用程序快两倍。





  static公共无效上传(字符串file1)
{
string upftpServerIP =ftp://ftp.domain.co.uk/lev/;
string upftpUserID =username;
string upftpPassword =password;

string uri = upftpServerIP + file1;
Uri serverUri = new Uri(uri);
if(serverUri.Scheme!= Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP =(FtpWebRequest)FtpWebRequest.Create(new Uri(upftpServerIP + file1));
reqFTP.Credentials = new NetworkCredential(upftpUserID,upftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = true;

Console.WriteLine(上传+ file1);

FileStream fs = File.OpenRead(@D:\LEV\+ file1);
byte [] buffer = new byte [fs.Length];
fs.Read(buffer,0,buffer.Length);
fs.Close();
Stream ftpstream = reqFTP.GetRequestStream();
ftpstream.Write(buffer,0,buffer.Length);
ftpstream.Close();


static public string [] GetFileListUpload()
{
string [] uploadFiles = Directory.GetFiles(@D:\LEV\, *。*,SearchOption.TopDirectoryOnly);
返回uploadFiles;


解决方案

这里:


  • 您的互联网连接不保证是对称的。大多数互联网连接计划(至少在我的区域)提供的上传带宽是下载带宽的1/8。

  • FTP服务器本身可能是限制传入连接的带宽。 FTP服务器也可能限制每次上传的最大带宽 。在这种情况下,您将从多线程上传中受益匪浅,一次上传多个文件。

I have written a console app to download files from an FTP and then upload to a different FTP location. The downloading on the files takes around 10 seconds, but the upload around 6 minutes. There are 256 files each around 5-30KB in size. So very small.

The upload and download code is very similar, it iterates through all files in the directory then uploads. It is fairly simple as seen below, it iterates and uploads files to the ftp from the D:\LEV\ folder.

EDIT: This is run on a Azure 'small' Windows virtual machine, so I assume bandwidth isn't a problem? Also I am performing the same task on another virtual machine using the windows ftp.exe to upload and it is 2 times quicker than my console app on the same machine.

Any clues why it is so slow, or are there ways to improve the speed?

static public void Upload(string file1)
{        
    string upftpServerIP = "ftp://ftp.domain.co.uk/lev/";
    string upftpUserID = "username";
    string upftpPassword = "password";

    string uri = upftpServerIP + file1;
    Uri serverUri = new Uri(uri);
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
       return;
    }
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(upftpServerIP + file1));
    reqFTP.Credentials = new NetworkCredential(upftpUserID, upftpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.Proxy = null;
    reqFTP.UsePassive = true;

    Console.WriteLine("Uploading " + file1);

    FileStream fs = File.OpenRead(@"D:\LEV\" + file1);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    fs.Close();
    Stream ftpstream = reqFTP.GetRequestStream();
    ftpstream.Write(buffer, 0, buffer.Length);
    ftpstream.Close();
}

static public string[] GetFileListUpload()
{
    string[] uploadFiles = Directory.GetFiles(@"D:\LEV\", "*.*", SearchOption.TopDirectoryOnly);
    return uploadFiles;
}

解决方案

There are several factors to consider here:

  • Your internet connection is not guaranteed to be symmetrical. Most internet connection plans (in my area, at least) offer an upload bandwidth which is 1/8th of the download bandwidth.

  • The FTP server itself may be limiting the bandwidth of incoming connections.

  • The FTP server may also be throttling the maximum bandwidth per upload. In this case, you will benefit greatly from multithreading the upload, uploading many files at once.

这篇关于为什么我的ftp上传方法如此缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆