Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c# - Azure功能使用FluentFTP将文件从FTP复制到Blob存储(Azure function to copy files from FTP to blob storage using FluentFTP)

I have an FTP source that adds daily file and I need to copy the files on daily basis from FTP to blob storage using FluentFTP library in azure function

(我有一个添加每日文件的FTP源,我需要每天在Azure函数中使用FluentFTP库将文件从FTP复制到Blob存储)

I am using C# in Azure function, and I did all the coding part but missing to download the file from FTP to copy it directly to the blob destination.

(我在Azure函数中使用C#,我做了所有编码部分,但是缺少从FTP下载文件以将其直接复制到Blob目标的操作。)

//#r "FluentFTP"
#r "Newtonsoft.Json"
#r "System.Data"
#r "Microsoft.WindowsAzure.Storage"

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Globalization;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using FluentFTP;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    string blobConnectionString = "ConnectionString";

    // Gestione BLOB Storage
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("out");

    using (FtpClient conn = new FtpClient()) 
    {
        conn.Host = "ftp server";
        conn.Credentials = new NetworkCredential("username", "pass");

        // get a list of files and directories in the "/OUT" folder
        foreach (FtpListItem item in conn.GetListing("/OUT")) 
        {
            // if this is a file and ends with CSV
            if (
            item.Type == FtpFileSystemObjectType.File
            &&
            item.FullName.ToLower().EndsWith(".csv")
            )
            {

                string yyyy = item.FullName.Substring(10,4);
                string mm   = item.FullName.Substring(14,2);
                string dd   = item.FullName.Substring(16,2);
                var fileName = "out/brt/" + yyyy + "/"+ mm + "/"+ dd + "/" + item.Name;

                CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
                // download the file
                conn.DownloadFile( blockBlob , item.FullName);
            }
        }

        return new OkObjectResult($"Hello");
    }
}

If I can use a blob container as a destination for the FluentFTP function then it would be the best, but this way I am getting the error that this blob block that I am using is not an a destination

(如果我可以将blob容器用作FluentFTP函数的目标,那将是最好的选择,但是这样我会收到以下错误消息:我正在使用的blob块不是目标)

This is the error that I am getting

(这是我得到的错误)

cannot convert from 'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob' to 'string'

(无法从“ Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob”转换为“字符串”)

I don't know if there is another way to download the file locally so I can upload it to the blob instead of using this client.DownloadFile function.

(我不知道是否还有另一种本地下载文件的方法,因此我可以将其上传到Blob,而不是使用此client.DownloadFile函数。)

  ask by Ahmed Essam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's probably not possible with Fluent FTP.

(Fluent FTP可能无法实现。)

There's FtpClient.Download method which takes Stream .

(有采用StreamFtpClient.Download方法。)

public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null)

But it does not look like there's API to get "blob upload Stream" .

(但是,看起来好像没有API可以获取“ blob upload Stream” 。)

Conversely, you cannot get "FTP download Stream" from FluentFTP (which you would be able to use with blob API).

(相反,您无法从FluentFTP获取“ FTP下载流” (您可以将其与blob API一起使用)。)


But you can use native .NET FtpWebRequest FTP client, which has API to get "FTP download Stream" :

(但是您可以使用本机.NET FtpWebRequest FTP客户端,该客户端具有用于获取“ FTP下载流”的 API:)

public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
    var ftpUserName = "xxxxxx";
    var ftpPassword = "xxxxxxxxx";
    var filename = "test.png";
    string blobConnectionString = "xxxxxxx";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("myblobcontainer");
    FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create("ftp://xxxxxx/" + filename);
    fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    fileRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
    FtpWebResponse fileResponse = (FtpWebResponse)fileRequest.GetResponse();
    Stream fileStream = fileResponse.GetResponseStream();
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);

    await blockBlob.UploadFromStreamAsync(fileStream);
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...