public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = "FileTracker/{filename}")] HttpRequest req, string filename, ILogger log) { log.LogInformation($"Retrieving File Tracker for {filename}"); return(new OkObjectResult(FileTrackerRepository.Get(filename))); }
public static async Task <string> Run( [ServiceBusTrigger("scan-for-virus")] string myQueueItem, [Blob("%AzureStorage:Container%", FileAccess.Read, Connection = "AzureStorage:ConnectionString")] CloudBlobContainer cloudBlobContainer, ILogger log) { log.LogInformation($"Scanning {myQueueItem}"); var config = new ConfigurationBuilder() .AddEnvironmentVariables() .Build(); // Download Blob Content var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(myQueueItem); var memoryStream = new MemoryStream(); await cloudBlockBlob.DownloadToStreamAsync(memoryStream); // Scan for Viruses var virusScanner = new CloudmersiveVirusScanner(); virusScanner.SetConfiguration(config); var stopWatch = new Stopwatch(); stopWatch.Start(); FileTrackerRepository.AddOperationResult(myQueueItem, "Virus Scan"); var scanResult = await virusScanner.ScanStreamAsync(memoryStream); stopWatch.Stop(); FileTrackerRepository.UpdateOperationResult(myQueueItem, "Virus Scan", stopWatch.ElapsedMilliseconds, false, scanResult.Message); log.LogInformation($"Scan Results for {myQueueItem}, Safe: {scanResult.IsSafe}, Message: {scanResult.Message}"); return(myQueueItem); }
public static async Task <string> Run( [ServiceBusTrigger("parse-and-validate")] string myQueueItem, [Blob("%AzureStorage:Container%", FileAccess.Read, Connection = "AzureStorage:ConnectionString")] CloudBlobContainer cloudBlobContainer, ILogger log) { log.LogInformation($"Parsing and validating: {myQueueItem}"); // Download Blob Content var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(myQueueItem); var memoryStream = new MemoryStream(); await cloudBlockBlob.DownloadToStreamAsync(memoryStream); // Parse and Validate File var stopWatch = new Stopwatch(); stopWatch.Start(); FileTrackerRepository.AddOperationResult(myQueueItem, "Parse and Validate"); var parseErrors = CsvFile.Validate(memoryStream); stopWatch.Stop(); FileTrackerRepository.UpdateOperationResult(myQueueItem, "Parse and Validate", stopWatch.ElapsedMilliseconds, true); if (parseErrors.Count > 0) { log.LogInformation($"Parsing and validating results for: {myQueueItem}, {string.Join(",", parseErrors)}"); throw new ApplicationException("File is not valid"); } else { log.LogInformation($"Parsing and validating results for: {myQueueItem}, looks good for next stage"); } return(myQueueItem); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, [Blob("%AzureStorage:Container%", FileAccess.Write, Connection = "AzureStorage:ConnectionString")] CloudBlobContainer cloudBlobContainer) { if (!(await cloudBlobContainer.ExistsAsync())) { await cloudBlobContainer.CreateAsync(); } var content = await req.ReadFormAsync(); var blobNames = new List <string>(); if (content.Files.Count > 0) { log.LogInformation("File Upload in progress!"); foreach (var file in content.Files) { var blobName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}".Replace("\"", ""); var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(blobName); var xff = req.Headers.FirstOrDefault(x => x.Key == "X-Forwarded-For").Value.FirstOrDefault(); blobNames.Add(blobName); cloudBlockBlob.Properties.ContentDisposition = file.ContentDisposition; cloudBlockBlob.Properties.ContentType = file.ContentType; cloudBlockBlob.Metadata.Add("originalName", file.FileName); if (!string.IsNullOrEmpty(xff)) { cloudBlockBlob.Metadata.Add("sourceIp", xff); } log.LogInformation($"Uploading {file.FileName} as {blobName}"); var stopWatch = new Stopwatch(); stopWatch.Start(); using (var fileStream = file.OpenReadStream()) { await cloudBlockBlob.UploadFromStreamAsync(fileStream); } stopWatch.Stop(); FileTrackerRepository.AddNew(file.FileName, blobName, stopWatch.ElapsedMilliseconds); // Add message in queue for next step var config = new ConfigurationBuilder() .AddEnvironmentVariables() .Build(); var serviceBusConnection = config["AzureWebJobsServiceBus"]; var queueName = "scan-for-virus"; var messageSender = new MessageSender(serviceBusConnection, queueName); var message = new Message(Encoding.UTF8.GetBytes(blobName)); await messageSender.SendAsync(message).ConfigureAwait(false); log.LogInformation($"Added message in {queueName}!"); } return(new OkObjectResult(new { blobs = blobNames })); } else { log.LogError("No Files to Upload!"); return(new BadRequestObjectResult("No files found in request.")); } }