/// <summary> /// Returns the blob event basis weeknumber, startdate and endDate /// </summary> /// <param name="weekNumber"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <returns></returns> public async Task <List <IEventData> > GetBLOBEvents(int year, int weekNumber, DateTime startDate, DateTime endDate) { //Get the table reference CloudTable employeeAuditTable = GetCloudTable(); var startDateTimeTicks = string.Format("{0:D19}", startDate.Ticks) + "_" + "ID"; var endDateTimeTicks = string.Format("{0:D19}", endDate.Ticks) + "_" + "ID"; var whereCondition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{year.ToString()}_{weekNumber.ToString()}"); var lessThanCondition = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, endDateTimeTicks); var greaterThanCondition = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, startDateTimeTicks); string query = TableQuery.CombineFilters(whereCondition, TableOperators.And, lessThanCondition); query = TableQuery.CombineFilters(query, TableOperators.And, greaterThanCondition); TableQuery <EventData> rangeQuery = new TableQuery <EventData>().Where(query); List <IEventData> blobEvents = new List <IEventData>(); // Print the fields for each customer. TableContinuationToken token = null; do { TableQuerySegment <EventData> resultSegment = await employeeAuditTable.ExecuteQuerySegmentedAsync(rangeQuery, token); token = resultSegment.ContinuationToken; foreach (EventData entity in resultSegment.Results) { if (!string.IsNullOrEmpty(entity.DestinationBlobInfoJSON)) { entity.DestinationBlobInfo = JsonConvert.DeserializeObject <DestinationBlobInfo>(entity.DestinationBlobInfoJSON); } entity.RecievedEventData = IBlobEvent.ParseBlobEvent(entity.RecievedEventDataJSON); blobEvents.Add(entity); } } while (token != null); return(blobEvents); }
/// <summary> /// Deletes the blob from the target / restore storage account /// </summary> /// <param name="eventData"></param> /// <returns></returns> public async Task <bool> DeleteBlobFromRestore(IBlobEvent eventData) { if (eventData is BlobEvent <DeletedEventData> ) { string destinationStorageAccountConnectionString = Environment.GetEnvironmentVariable("SRC_STORAGE_ACCOUNT_CONN"); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <DeletedEventData> deletedEventData = (BlobEvent <DeletedEventData>)eventData; //creating sourceBlockBlob to get the container name and blob name CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(deletedEventData.data.url)); CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(sourceBlockBlob.Container.Name); bool destinationContainerExists = await destinationContainer.ExistsAsync(); if (destinationContainerExists) { CloudBlockBlob blockBlobToDelete = destinationContainer.GetBlockBlobReference(sourceBlockBlob.Name); bool blobDeleted = await blockBlobToDelete.DeleteIfExistsAsync(); if (blobDeleted) { _logger.LogInformation($"Successfully deleted blob: {sourceBlockBlob.Name} in destination (restore) storage account."); } else { _logger.LogInformation($"Failed to delete blob: {sourceBlockBlob.Name} in destination (restore) storage account."); } return(blobDeleted); } else { _logger.LogInformation($"Not able to locate the container: {sourceBlockBlob.Container.Name} in destination (restore) storage account."); } } return(false); }
/// <summary> /// Event Data Constructor /// </summary> /// <param name="eventData"></param> public EventData(string eventData) { string eventId; DateTime eventDateTime; ReceivedEventData = IBlobEvent.ParseBlobEvent(eventData, out eventId, out eventDateTime); if (ReceivedEventData != null) { ReceivedEventDataJSON = JsonConvert.SerializeObject(ReceivedEventData); EventDateDetails dateDetails = new EventDateDetails(eventDateTime); base.PartitionKey = $"{dateDetails.year}_{dateDetails.WeekNumber}"; base.RowKey = $"{dateDetails.formattedDate}_{eventId.Replace("-", "")}"; } }
/// <summary> /// CopyBlobFromSourceToBackup: /// - Copies blob from source SA container to destination/backup SA container /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = Environment.GetEnvironmentVariable("RES_STORAGE_ACCOUNT_CONN"); string sourceStorageAccountConnectionString = Environment.GetEnvironmentVariable("SRC_STORAGE_ACCOUNT_CONN"); if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; destinationBlobName += "."; destinationBlobName += DateTimeUtil.GetString; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); _logger.LogInformation($"About to sync copy from Container: {sourceBlockBlob.Container.Name}, Blob: {sourceBlockBlob.Name}. Blob size: {sourceBlockBlob.Properties.Length} bytes"); // copyResult = "SYNCCOPY"; string copyResult = await destinationBlob.StartCopyAsync(sourceBlockBlob); destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; _logger.LogInformation($"Copy Scheduled. Source Blob Name: {sourceBlockBlob.Name}, Destination Blob Name: {destinationBlobInfo.BlobName}, Copy Id: {copyResult}."); return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name: {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }
/// <summary> /// CopyBlobFromSourceToBackup: /// - Copies blob from source SA container to destination/backup SA container /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = Environment.GetEnvironmentVariable("RES_STORAGE_ACCOUNT_CONN"); string sourceStorageAccountConnectionString = Environment.GetEnvironmentVariable("SRC_STORAGE_ACCOUNT_CONN"); int blobTier = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TargetBlobTier")) ? 2 : int.Parse(Environment.GetEnvironmentVariable("TargetBlobTier")); StandardBlobTier targetBlobTier; // Default tier is set to Cool switch (blobTier) { case 1: targetBlobTier = StandardBlobTier.Hot; break; case 2: targetBlobTier = StandardBlobTier.Cool; break; case 3: targetBlobTier = StandardBlobTier.Archive; break; default: targetBlobTier = StandardBlobTier.Cool; break; } ; if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; destinationBlobName += "."; destinationBlobName += DateTimeUtil.GetString; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); _logger.LogInformation($"About to sync copy from Container: {sourceBlockBlob.Container.Name}, Blob: {sourceBlockBlob.Name}. Blob size: {sourceBlockBlob.Properties.Length} bytes"); // copyResult = "SYNCCOPY"; string copyResult = await destinationBlob.StartCopyAsync( sourceBlockBlob, targetBlobTier, null, // Rehydrate priority null, // Source access condition null, // Destination access condition null, // Blob request options null, // Operation context (new CancellationTokenSource()).Token); destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; _logger.LogInformation($"Copy Scheduled. Source Blob Name: {sourceBlockBlob.Name}, Destination Blob Name: {destinationBlobInfo.BlobName}, Copy Id: {copyResult}."); return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name: {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }
/// <summary> /// CopyBlobFromSourceToBackup /// </summary> /// <returns></returns> public async Task <DestinationBlobInfo> CopyBlobFromSourceToBackup(IBlobEvent eventData) { DestinationBlobInfo destinationBlobInfo = null; string destinationStorageAccountConnectionString = _config.GetConnectionString("BackupBlobStorage"); string sourceStorageAccountConnectionString = _config.GetConnectionString("SourceBlobStorage"); bool isServerCopy = bool.Parse(_config.GetSection("AppSettings")["IsServerCopy"]); if (eventData is BlobEvent <CreatedEventData> ) { // Retrieve the storage account from the connection string. CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorageAccountConnectionString); CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient(); // Retrieve the storage account from the connection string. CloudStorageAccount destinationStorageAccount = CloudStorageAccount.Parse(destinationStorageAccountConnectionString); CloudBlobClient destinationBlobClient = destinationStorageAccount.CreateCloudBlobClient(); BlobEvent <CreatedEventData> createdEventData = (BlobEvent <CreatedEventData>)eventData; string url = createdEventData.data.url; CloudBlockBlob sourceBlockBlob = new CloudBlockBlob(new Uri(url), sourceBlobClient); bool sourceBlobExists = await sourceBlockBlob.ExistsAsync(); if (sourceBlobExists) { long blobSize = sourceBlockBlob.Properties.Length; EventDateDetails dateDetails = new EventDateDetails(createdEventData.eventTime); string destinationContaninerName = dateDetails.year.ToString(); string destinationBlobName = $"wk{dateDetails.WeekNumber}/dy{(int)dateDetails.DayOfWeek}/{sourceBlockBlob.Container.Name}/{sourceBlockBlob.Name}"; CloudBlobContainer destinationContainer = destinationBlobClient.GetContainerReference(destinationContaninerName); bool result = await destinationContainer.CreateIfNotExistsAsync(); CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(destinationBlobName); string copyResult = string.Empty; if (isServerCopy) { string blobToken = GenerateSASBlobToken(sourceBlockBlob); _logger.LogInformation($"About to server copy {sourceBlockBlob.Name}. Blob size {sourceBlockBlob.Properties.Length} bytes"); copyResult = await destinationBlob.StartCopyAsync(new Uri(sourceBlockBlob.Uri.AbsoluteUri + blobToken)); } else { _logger.LogInformation($"About to sync copy {sourceBlockBlob.Name}. Blob size {sourceBlockBlob.Properties.Length} bytes"); copyResult = "SYNCCOPY"; await TransferManager.CopyAsync(sourceBlockBlob, destinationBlob, false); } destinationBlobInfo = new DestinationBlobInfo(); destinationBlobInfo.ContainerName = destinationContainer.Name; destinationBlobInfo.BlobName = destinationBlobName; destinationBlobInfo.CopyReferenceId = copyResult; destinationBlobInfo.OrgContainerName = sourceBlockBlob.Container.Name; destinationBlobInfo.OrgBlobName = sourceBlockBlob.Name; return(destinationBlobInfo); } else { _logger.LogInformation($"Not able to locate the block blob in source storage account---Block blob Name {sourceBlockBlob.Name}"); } } else { _logger.LogInformation($"Input event data is not of Created Event Type."); } return(destinationBlobInfo); }