将文件从FTP直接传输到C#中的Azure Blob存储 [英] Transferring files from FTP directly to Azure Blob storage in C#

查看:89
本文介绍了将文件从FTP直接传输到C#中的Azure Blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a function which move files from FTP server to Azure Blob storage. I want to pass the stream from FTP to blob so that I can upload the files. I am running a while loop for every file and trying to move the file to blob storage using UploadFromStreamAsync(). But when I came to this call, my stream object gets disposed because of which file is getting transfer to blob but without any content. I do not want to dispose my stream object till all files are transfer. Can anyone tell me what's wrong going on??

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();
    }
}

推荐答案

您正在请求FTP服务器上的文件夹的目录列表.而与此同时,您也可以使用列表:

You are requesting directory listing of a folder on FTP server. And with the listing you are, at the same time:

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

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

That can never work. And moreover it makes no sense.

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

I assume that you actually want to upload the files, not the listing.

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

For that you need to start downloading the individual files from the FTP server in your loop:

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);

这篇关于将文件从FTP直接传输到C#中的Azure Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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