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
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.
//#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
This is the error that I am getting
cannot convert from 'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob' to 'string'
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.