C# Azure函数使用FluentFTP将文件从FTP复制到blob存储
我有一个添加每日文件的FTP源,我需要每天使用azure函数中的FluentFTP库将文件从FTP复制到blob存储 我正在使用Azure函数中的C#,我完成了所有的编码部分,但没有从FTP下载文件,将其直接复制到blob目标C# Azure函数使用FluentFTP将文件从FTP复制到blob存储,c#,ftp,azure-functions,azure-storage-blobs,fluentftp,C#,Ftp,Azure Functions,Azure Storage Blobs,Fluentftp,我有一个添加每日文件的FTP源,我需要每天使用azure函数中的FluentFTP库将文件从FTP复制到blob存储 我正在使用Azure函数中的C#,我完成了所有的编码部分,但没有从FTP下载文件,将其直接复制到blob目标 /#r“FluentFTP” #r“Newtonsoft.Json” #r“系统数据” #r“Microsoft.WindowsAzure.Storage” 使用Newtonsoft.Json; 使用Newtonsoft.Json.Linq; 使用System.Colle
/#r“FluentFTP”
#r“Newtonsoft.Json”
#r“系统数据”
#r“Microsoft.WindowsAzure.Storage”
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用System.Collections.Generic;
利用制度全球化;
使用制度;
使用系统配置;
使用系统数据;
使用System.Data.SqlClient;
使用Microsoft.WindowsAzure.Storage;
使用Microsoft.WindowsAzure.Storage.Auth;
使用Microsoft.WindowsAzure.Storage.Blob;
Net系统;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Primitives;
使用Newtonsoft.Json;
使用FluentFTP;
公共静态异步任务运行(HttpRequest请求,ILogger日志)
{
string blobConnectionString=“ConnectionString”;
//GestioneBlob存储
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(blobConnectionString);
CloudBlobClient blobClient=storageAccount.CreateCloudBlobClient();
CloudBlobContainer container=blobClient.GetContainerReference(“out”);
使用(FtpClient conn=new FtpClient())
{
conn.Host=“ftp服务器”;
连接凭证=新网络凭证(“用户名”、“通行证”);
//获取“/OUT”文件夹中的文件和目录列表
foreach(在conn.GetListing(“/OUT”)中的FtpListItem项)
{
//如果这是一个文件并以CSV结尾
如果(
item.Type==FtpFileSystemObjectType.File
&&
item.FullName.ToLower().EndsWith(“.csv”)
)
{
字符串yyy=item.FullName.Substring(10,4);
字符串mm=item.FullName.Substring(14,2);
字符串dd=item.FullName.Substring(16,2);
var fileName=“out/brt/”+yyy+“/”+mm+“/”+dd+“/”+item.Name;
CloudBlockBlob blockBlob=container.getBlockBlockBlobReference(文件名);
//下载该文件
conn.DownloadFile(blockBlob,item.FullName);
}
}
返回新的OkObjectResult($“Hello”);
}
}
如果我可以使用blob容器作为FluentFTP函数的目标,那么这将是最好的,但这样我得到的错误是,我使用的blob块不是目标
这就是我得到的错误
无法从“Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob”转换为“string”
我不知道是否有其他方法可以在本地下载该文件,以便我可以将其上载到blob,而不是使用此
客户端.DownloadFile
功能。您不能使用SDK直接将文件从FTP移动到blob。您必须先临时下载这些文件-要么下载到流中(取决于文件的大小),要么下载到临时文件中
但是,如果您只想按计划将文件从FTP移动到Blob,我实际上会考虑使用Azure Data Factory,它完全是为这样的任务而构建的:您不能使用SDK直接将文件从FTP移动到Blob。您必须先临时下载这些文件-要么下载到流中(取决于文件的大小),要么下载到临时文件中
然而,如果您只想按计划将文件从FTP移动到Blob,我实际上会考虑使用Azure Data Factory,它完全是为这样的任务而构建的:使用Fluent FTP可能不可能 有
FtpClient.Download
方法,它采用Stream
public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null)
使用流畅的FTP可能不可能做到这一点 有
FtpClient.Download
方法,它采用Stream
public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null)
如果要使用FluentFTP,可以使用以下两种方法之一获取blob上载流: 然后您可以使用
FTPClient.Download
方法,该方法采用流
public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null)
public bool下载(流外扩、字符串远程路径、IProgress progress=null)
大概是这样的:
[FunctionName("HttpTriggerCSharp")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var ftpHost = "xxxxx";
var ftpUserName = "xxxx";
var ftpPassword = "xxxx";
var filename = "xxxxx";
string blobConnectionString = "xxxxxxxxxxx";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("xxxxx");
FtpClient client = new FtpClient(ftpHost, ftpUserName, ftpPassword); // or set Host & Credentials
client.EncryptionMode = FtpEncryptionMode.Implicit;
client.SslProtocols = SslProtocols.None;
client.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
client.Connect();
void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
// add logic to test if certificate is valid here
e.Accept = true;
}
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
var outStream = blockBlob.OpenWrite();
client.Download(outStream,filename);
outStream.Commit(); // I'm not sure if this is needed?
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
return (ActionResult)new OkObjectResult($"Action succeeded.");
}
[FunctionName(“HttpTriggerCSharp”)]
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
var ftpHost=“xxxxx”;
var ftpUserName=“xxxx”;
var ftpPassword=“xxxx”;
var filename=“xxxxx”;
字符串blobConnectionString=“xxxxxxxxxx”;
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(blobConnectionString);
CloudBlobClient blobClient=storageAccount.CreateCloudBlobClient();
CloudBlobContainer容器=blobClient.GetContainerReference(“xxxxx”);
FtpClient client=新的FtpClient(ftpHost、ftpUserName、ftpPassword);//或设置主机和凭据
client.EncryptionMode=FtpEncryptionMode.Implicit;
client.SslProtocols=SslProtocols.None;
client.ValidateCertificate+=新的FtpSslValidation(OnValidateCertificate);
client.Connect();
验证证书无效(FTP客户端控制、FtpSslValidationEventArgs e){
//在此处添加逻辑以测试证书是否有效
e、 接受=真;
}
CloudBlockBlob blockBlob=container.getBlockBlockBlobReference(文件名);
var outStream=blockBlob.OpenWrite();
下载(扩展,文件名);
outStream.Commit();//我不确定是否需要这样做?
log.LogInformation($“C#计时器触发器函数在:{DateTime.Now}执行”);
返回(ActionResult)新的OkObjectResult($“操作成功”);
}
如果您想使用FluentFTP,可以使用以下tw之一获取blob上载流