public async Task <List <ActionResponse> > BulkUploadBytesAsync(List <DocumentDetail> documentDetails) { return(await Task.Run(() => { List <ActionResponse> actionResponses = new List <ActionResponse>(); //parallel Parallel.ForEach(documentDetails, (a) => { try { if (a.Bytes != null && a.Bytes.Length > 0) { actionResponses.Add(Task.Run(async() => await UploadBlockAsync(a.Name, a.Bytes, a.PartitionName, a.MetaData, null, DocumentContentType.Byte)).Result); } else { actionResponses.Add(ActionResponse.FailResponse(a.Name, new ErrorCodes { Message = Constants.DocumentContentNotFoundMessage, Code = Constants.DocumentContentNotFoundCode })); } } catch (Exception exception) { _logger.LogError(exception, $"Unable to upload {a.Name} document"); actionResponses.Add(ActionResponse.FailResponse(a.Name, exception)); } }); return actionResponses; })); }
public async Task <ActionResponse> DeleteAsync(string fileName, string partitionName) { CloudBlockBlob blockBlob = null; bool blobExist = false; bool deleteResult = false; ActionResponse result = null; var blockName = GetBlockName(partitionName, fileName); try { var policyResult = Policy .Handle <Exception>() .WaitAndRetryAsync( _maxRetryValueForBlobAction, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => { _logger.LogError($"Retey {retryCount} after {timeSpan.TotalSeconds} of Blob {exception.Message}"); } ); await policyResult.ExecuteAsync( async() => { if (blockBlob == null) { blockBlob = await GetBlockBlobAsync(_containerName, blockName); blobExist = await blockBlob.ExistsAsync(); } if (blockBlob != null && blobExist) { deleteResult = await blockBlob.DeleteIfExistsAsync(); result = deleteResult ? ActionResponse.SuccessResponse(blockName) : ActionResponse.FailResponse(blockName, new ErrorCodes { Message = Constants.SomethingWentWrongMessage, Code = Constants.SomethingWentWrongCode }); } else if (!blobExist) { result = ActionResponse.FailResponse(blockName, new ErrorCodes { Message = string.Format(Constants.BlobDoesNotExistMessage, blockName, _accountName), Code = Constants.BlobDoesNotExistCode }); } }); } catch (Exception ex) { _logger.LogError(ex, $"Unable to delete {blockName}"); throw ex; } return(result); }
private async Task <ActionResponse> UploadBlockAsync(string fileName, byte[] fileBytes, string partitionName, Dictionary <string, string> metaData, string data, DocumentContentType documentContentType) { CloudBlockBlob blockBlob = null; var blobUri = string.Empty; var blockName = GetBlockName(partitionName, fileName); var result = Policy .Handle <Exception>() .WaitAndRetryAsync( _maxRetryValueForBlobAction, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => { _logger.LogError($"Retey {retryCount} after {timeSpan.TotalSeconds} of Blob {exception.Message}"); } ); await result.ExecuteAsync( async() => { if (blockBlob == null) { blockBlob = await GetBlockBlobAsync(_containerName, blockName); } if (blockBlob != null) { await blockBlob.DeleteIfExistsAsync(); switch (documentContentType) { case DocumentContentType.Byte: await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length); break; case DocumentContentType.Text: await blockBlob.UploadTextAsync(data); break; default: break; } if (metaData != null) { foreach (var keyValue in metaData) { if (blockBlob.Metadata.ContainsKey(keyValue.Key)) { blockBlob.Metadata.Remove(keyValue); } blockBlob.Metadata.Add(keyValue); } await blockBlob.SetMetadataAsync(); } blobUri = blockBlob.Uri.ToString(); } }); return(!string.IsNullOrEmpty(blobUri) ? ActionResponse.UploadSuccessResponse(fileName, blobUri) : ActionResponse.FailResponse(fileName, new ErrorCodes { Message = Constants.SomethingWentWrongMessage, Code = Constants.SomethingWentWrongCode })); }