基本上我创建了一个azure webjob,它将从sql表中循环记录,该表具有sftp位置详细信息。对于每个sftp位置,使用sftpclient连接并读取文件夹中的所有文件。 对于每个文件,我连接到azure并将其存储在blob中。
我在单个文件中完成了上述操作。但希望以适当的面向对象的方式做。不确定什么是正确的方法。我对设计模式的经验不是很多,但如果有人能够推荐正确的方法,我会很高兴。
感谢是否有人可以帮助我以适当的面向对象的方式实现这一目标。
由于
var azureBlob = AzureBlobStorage.Instance(StorageConnectionString, Containername);
List<SftpLocationData> sftps = null;
try
{
sftps = SftpLocationClient.GetSftpLocationDetails().ToList();
if (sftps == null || !sftps.Any()) return;
}
catch (Exception ex)
{
LogMessage(string.Format("Error getting sftp details : {0}", ex.Message), log);
}
foreach (var fileClient in sftps.Select(sftp => new FileTransferClient(sftp)))
{
using (var sftpClient = fileClient.CreateClient())
{
sftpClient.Connect();
var files = sftpClient.ListDirectory(path: fileClient.Data.Directory ?? ".").ToList();
if (files.Any())
{
var validFiles = files.Where(f => ext.Any(e => e == Path.GetExtension(f.Name))).ToList();
foreach (var file in validFiles)
{
var fileExists = azureBlob.FileExists(file.Name);
var blobUri = string.Empty;
var blobName = file.Name;
var fileImport = PopulateFileImportData(file);
if (fileExists)
{
int count = azureBlob.ListFiles(blobName);
blobName = (count == 0) ? blobName : String.Format("{0}_v{1}", blobName, count);
fileImport.Error = true;
fileImport.ErrorMsg = "Duplicate File";
}
fileImport.FileName = blobName;
try
{
var fileSaved = RbsClient.SaveFileImport(fileImport);
blobUri = azureBlob.UploadFile(fileSaved.FileName, sftpClient.OpenRead(file.FullName));
fileSaved.Archived = DateTime.Now;
RRClient.UpdateFile(fileSaved);
}
catch (Exception ex)
{
LogMessage(string.Format("Error saving fileimport detail : {0}", ex.Message), log);
}
if (fileImport.Error) continue;
IQueueGenerator queueGenerator = new QueueGenerator();
var queueName = queueGenerator.GetQueueName(
blobName,
fileClient.Data,
blobUri);
if (string.IsNullOrEmpty(queueName)) continue;
}
}
sftpClient.Disconnect();
}
}
}
catch (Exception ex)
{
LogMessage(string.Format("Error occurred in processing pending altapay requests. Error : {0}", ex.Message), log);
}
答案 0 :(得分:0)
根据我的理解,您需要将文件/文件夹从FTP复制到azure blob存储。
Step 1. Get list of SFTP locations
Step 2. Init Azure Blob Storage Connection
Step 3. Init SFTP
Step 4. Iterate Files in SFTP
Step 5. Store the file into Blob Storage
Step 6. Close SFTP connection
Step 7. Close Azure Blob Storage