System.Net.FtpClient如何进行异步下载

时间:2014-04-24 21:18:14

标签: c# azure ftp

我有一些便宜的托管服务,非常适合我的需求,但当然有便宜的托管,有一天他们可能会消失(虽然他们已经营业了好几年)所以我想我可以上传我的备份到blob存储什么是非常便宜的。

我的计划是让一个石英作业在我的托管网站上运行并连接到我的ftp并下载该文件,然后将其上传到我的blob容器中。

我想做这个异步,似乎System.Net.FtpClient有这个选项,但我对如何使用它很困惑。

我在他们的例子中发现了这一点但却被它弄糊涂了。

public static class BeginOpenReadExample {
        static ManualResetEvent m_reset = new ManualResetEvent(false);

        public static void BeginOpenRead() {
            // The using statement here is OK _only_ because m_reset.WaitOne()
            // causes the code to block until the async process finishes, otherwise
            // the connection object would be disposed early. In practice, you
            // typically would not wrap the following code with a using statement.
            using (FtpClient conn = new FtpClient()) {
                m_reset.Reset();

                conn.Host = "localhost";
                conn.Credentials = new NetworkCredential("ftptest", "ftptest");
                conn.BeginOpenRead("/path/to/file",
                    new AsyncCallback(BeginOpenReadCallback), conn);

                m_reset.WaitOne();
                conn.Disconnect();
            }
        }

        static void BeginOpenReadCallback(IAsyncResult ar) {
            FtpClient conn = ar.AsyncState as FtpClient;

            try {
                if (conn == null)
                    throw new InvalidOperationException("The FtpControlConnection object is null!");

                using (Stream istream = conn.EndOpenRead(ar)) {
                    byte[] buf = new byte[8192];

                    try {
                        DateTime start = DateTime.Now;

                        while (istream.Read(buf, 0, buf.Length) > 0) {
                            double perc = 0;

                            if (istream.Length > 0)
                                perc = (double)istream.Position / (double)istream.Length;

                            Console.Write("\rTransferring: {0}/{1} {2}/s {3:p}         ",
                                          istream.Position.FormatBytes(),
                                          istream.Length.FormatBytes(),
                                          (istream.Position / DateTime.Now.Subtract(start).TotalSeconds).FormatBytes(),
                                          perc);
                        }
                    }
                    finally {
                        Console.WriteLine();
                        istream.Close();
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            finally {
                m_reset.Set();
            }
        }
    }

" BeginOpenReadCallback"区域让我困惑,我不明白为什么他们打电话给#34; EndOpenRead"马上阅读(想想会在阅读流之后)。

看起来他们在阅读方面做得不多,我不确定在读入后我应该存储流数据。

我看到azure blob sdk有这个

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
} 

所以不确定我是否可以将ftp读取与此UploadFromStream方法合并。

0 个答案:

没有答案