将文件从FTP直接传输到C#中的Azure Blob存储

时间:2018-12-21 06:56:59

标签: c# .net ftp azure-storage-blobs ftpwebrequest

我编写了一个将文件从FTP服务器移动到Azure Blob存储的功能。我想将流从FTP传递到Blob,以便可以上传文件。我正在为每个文件运行while循环,并尝试使用UploadFromStreamAsync()将文件移至Blob存储。但是,当我进行此调用时,我的流对象被处置了,因为哪个文件正在传输到blob,但是没有任何内容。在传输所有文件之前,我不希望处置流对象。谁能告诉我怎么了?

public static async Task FTP_TO_BLOB_TRANSFER()
{
    string ftpPath = ConfigurationSettings.AppSettings.Get("ftpPath");
    string ftpUserName = ConfigurationSettings.AppSettings.Get("ftpUserName");
    string ftpPassword = ConfigurationSettings.AppSettings.Get("ftpPassword");

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);

    string connectionString = ConfigurationSettings.AppSettings.Get("connectionString");
    string folderName = "Inbox/";
    string file = reader.ReadLine();
    while (!string.IsNullOrEmpty(file))
    {

        string fileName = Path.GetFileNameWithoutExtension(file);
        string guid = Guid.NewGuid().ToString();
        string extension = Path.GetExtension(file);
        try
        {
            Stream fileForBlobStorage = reader.BaseStream;
            if (CloudStorageAccount.TryParse(connectionString, out storageAccount))
            {
                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("falcon");
                BlobContainerPermissions permissions = new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                await cloudBlobContainer.SetPermissionsAsync(permissions);

                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(folderName + fileName + "-" + '[' + guid + ']' + guid + extension.ToString());

                await cloudBlockBlob.UploadFromStreamAsync((Stream  )fileForBlobStorage);
            }
            else
            {
                Console.WriteLine("Connection string not defined.");
            }
        }
        catch (Exception e)
        {
            string message = e.Message;
            Console.WriteLine(message);
        }
        file = reader.ReadLine();
    }
}

1 个答案:

答案 0 :(得分:2)

您正在请求FTP服务器上的文件夹的目录列表。有了清单,您就可以同时使用:

  • 逐行读取列表(逐个文件)-以某种方式尝试处理各个行/文件。
  • 但是,您正在尝试将列表(相同的流)上传到Blob。

那永远都行不通。而且这没有任何意义。

我假设您实际上是要上传文件,而不是列表。

为此,您需要在循环中开始从FTP服务器下载单个文件:

FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create(ftpPath + file);
fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
fileRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
FtpWebResponse fileResponse = (FtpWebResponse)fileRequest.GetResponse();
Stream fileStream = fileResponse.GetResponseStream();

await cloudBlockBlob.UploadFromStreamAsync(fileStream);