public static async Task <UploadResultDto> UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig) { // Create storagecredentials object by reading the values from the configuration (appsettings.json) StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey); // Create cloudstorage account by passing the storagecredentials CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json) CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.FileHandlerContainer); // Get the reference to the block blob from the container CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); // Upload the file await blockBlob.UploadFromStreamAsync(fileStream); UploadResultDto result = new UploadResultDto() { FileUrl = blockBlob.Uri.AbsoluteUri, Success = true }; return(await Task.FromResult(result)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [StorageAccount("StorageConnectionString")] CloudStorageAccount storageAccount, TraceWriter log ) { var client = storageAccount.CreateCloudTableClient(); string partitionKey = await Authenticator.Authenticate(client, req); if (string.IsNullOrEmpty(partitionKey)) { return(new UnauthorizedResult()); } string json = await req.ReadAsStringAsync(); var items = JsonConvert.DeserializeObject <List <ItemJson> >(json); if (items == null) { return(new BadRequestObjectResult("Could not correctly parse the request parameters")); } else if (items.Count > 100) { return(new BadRequestObjectResult("Too many items to store, maximum 100 per call")); } else if (items.Count <= 0) { return(new BadRequestObjectResult("No items to store")); } var partition = await PartionUtility.GetUploadPartitionV2(log, client, partitionKey); log.Info($"Received a request from {partitionKey} to upload {items.Count} items to {partition.RowKey}"); var itemTable = client.GetTableReference(ItemV2.TableName); await itemTable.CreateIfNotExistsAsync(); var itemMapping = await Insert(partitionKey, partition.RowKey, itemTable, items); log.Info($"Inserted {items.Count} items over {itemMapping.Count} batches"); bool shouldClosePartition = items.Count >= 90; if (shouldClosePartition) { log.Info($"Closing partition {partition.PartitionKey}, due to received count at {items.Count}"); partition.IsActive = false; await client.GetTableReference(PartitionV2.TableName).ExecuteAsync(TableOperation.Replace(partition)); } var result = new UploadResultDto { Partition = itemMapping.First().Partition, IsClosed = shouldClosePartition, Items = itemMapping }; return(new OkObjectResult(result)); }
public async Task <ActionResult <UploadResultDto> > UploadAsync([FromForm] IFormFile file) { if (file == null || file.Length == 0) { throw new ArgumentException("The specified file is null or empty.", nameof(file)); } if (!_allowedFileExtensions.Contains(Path.GetExtension(file.FileName))) { throw new ArgumentException("The specified file has invalid extension.", nameof(file)); } byte[] bytes; await using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); bytes = memoryStream.ToArray(); } IFileReader fileReader; if (Path.GetExtension(file.FileName) == ".csv") { fileReader = new CsvFileReader(); } else { fileReader = new XmlFileReader(); } var parseResult = await _uploadService.UploadAsync <ParseResult>(bytes, fileReader); var result = new UploadResultDto(); if (parseResult.IsSucceeded) { result.Info.Add($"Uploaded {parseResult.TransactionData.Count()} Transactions"); } else { result.Errors = parseResult.Errors.ToList(); } result.Warnings = parseResult.Warnings.ToList(); return(Ok(result)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [StorageAccount("StorageConnectionString")] CloudStorageAccount storageAccount, TraceWriter log ) { var client = storageAccount.CreateCloudTableClient(); string partitionKey = await Authenticator.Authenticate(client, req); if (string.IsNullOrEmpty(partitionKey)) { return(new UnauthorizedResult()); } string json = await req.ReadAsStringAsync(); var items = JsonConvert.DeserializeObject <List <ItemJsonV3> >(json); if (items == null) { return(new BadRequestObjectResult("Could not correctly parse the request parameters")); } else if (items.Count > 100) { return(new BadRequestObjectResult("Too many items to store, maximum 100 per call")); } else if (items.Count <= 0) { return(new BadRequestObjectResult("No items to store")); } else if (items.Select(item => item.RemotePartition).Distinct().Count() > 1) { return(new BadRequestObjectResult("Cannot store items from multiple partitions in a single call")); } // Just ensuring that a partition entry exists, so that we can find it again later. var partition = await PartionUtility.GetUploadPartitionV3(log, client, partitionKey, items.First().RemotePartition); log.Info($"Received a request from {partitionKey} to upload {items.Count} items to {partition.RowKey}"); var itemTable = client.GetTableReference(ItemV2.TableName); await itemTable.CreateIfNotExistsAsync(); var itemMapping = await Insert(log, partition.RowKey, itemTable, items); log.Info($"Inserted {items.Count} items"); bool shouldClosePartition = items.Count >= 90; if (shouldClosePartition) { log.Info($"Closing partition {partition.PartitionKey}, due to received count at {items.Count}"); partition.IsActive = false; await client.GetTableReference(PartitionV2.TableName).ExecuteAsync(TableOperation.Replace(partition)); } var result = new UploadResultDto { Partition = partition.RowKey.Remove(0, partitionKey.Length), // Strip the owner email from the partition IsClosed = shouldClosePartition, }; return(new OkObjectResult(result)); }