/// <summary> /// Checks the blob to see if it can be archived. /// </summary> /// <param name="blob"></param> /// <param name="filterCriteria"></param> /// <returns></returns> private static bool DoesBlobMatchFilterCriteria(CloudBlockBlob blob, Models.FilterCriteria filterCriteria) { if (blob.Properties.StandardBlobTier == StandardBlobTier.Archive) { return(false); } var dateTimeFrom = filterCriteria.LastModifiedDateFrom ?? DateTime.MinValue; var dateTimeTo = filterCriteria.LastModifiedDateTo ?? DateTime.MaxValue; var minBlobSize = filterCriteria.MinBlobSize; bool isDateTimeCheckPassed = false; if (blob.Properties.LastModified.HasValue) { var lastModified = blob.Properties.LastModified.Value.DateTime; if (dateTimeFrom <= lastModified && dateTimeTo >= lastModified) { isDateTimeCheckPassed = true; } } bool isBlobSizeCheckPassed = blob.Properties.Length >= minBlobSize; return(isDateTimeCheckPassed || isBlobSizeCheckPassed); }
public static Models.FolderStatistics AnalyzeFolder(string folderName, Models.FilterCriteria filterCriteria) { Models.FolderStatistics folderStatistics = new Models.FolderStatistics(folderName); try { var files = ListFiles(folderName); var filesCount = files.LongCount(); var totalFilesSize = files.Sum(f => f.Length); var lastModifiedDateFrom = filterCriteria.LastModifiedDateFrom ?? DateTime.MinValue; var lastModifiedDateTo = filterCriteria.LastModifiedDateTo ?? DateTime.MaxValue; var filteredFiles = files.Where(f => f.Length >= filterCriteria.MinBlobSize && f.LastWriteTime >= lastModifiedDateFrom && f.LastWriteTime <= lastModifiedDateTo); var filteredFilesCount = filteredFiles.LongCount(); var filteredFilesSize = filteredFiles.Sum(f => f.Length); folderStatistics.FilesStatistics.Count = filesCount; folderStatistics.FilesStatistics.Size = totalFilesSize; folderStatistics.MatchingFilesStatistics.Count = filteredFilesCount; folderStatistics.MatchingFilesStatistics.Size = filteredFilesSize; return(folderStatistics); } catch (Exception) { return(folderStatistics); } }
/// <summary> /// Analyzes blobs in a container. This method will list all blobs in a blob container and /// matches them against the filter criteria. /// </summary> /// <param name="containerName">Name of the blob container.</param> /// <param name="filterCriteria"><see cref="Models.FilterCriteria"/></param> /// <returns> /// <see cref="Models.ContainerStatistics"/> which contains information about block blobs /// in different tiers and puts them in 2 buckets - all block blobs & block blobs that match /// the filter criteria. /// </returns> public static async Task <Models.ContainerStatistics> AnalyzeContainer(string containerName, Models.FilterCriteria filterCriteria) { var blobContainer = _blobClient.GetContainerReference(containerName); var containerStats = new Models.ContainerStatistics(containerName); BlobContinuationToken token = null; try { do { var result = await blobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, 250, token, null, null); token = result.ContinuationToken; var blobs = result.Results; foreach (var blob in blobs) { var cloudBlockBlob = blob as CloudBlockBlob; if (cloudBlockBlob != null) { long blobSize = cloudBlockBlob.Properties.Length; DateTime blobLastModifiedDate = cloudBlockBlob.Properties.LastModified.Value.DateTime; var doesBlobMatchFilterCriteria = DoesBlobMatchFilterCriteria(cloudBlockBlob, filterCriteria); var blobTier = cloudBlockBlob.Properties.StandardBlobTier; switch (blobTier) { case null: case StandardBlobTier.Hot: var hotAccessTierStats = containerStats.BlobsStatistics[StandardBlobTier.Hot]; hotAccessTierStats.Count += 1; hotAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingHotAccessTierStats = containerStats.MatchingBlobsStatistics[StandardBlobTier.Hot]; matchingHotAccessTierStats.Count += 1; matchingHotAccessTierStats.Size += blobSize; matchingHotAccessTierStats.BlobNames.Add(cloudBlockBlob.Name); } break; case StandardBlobTier.Cool: var coolAccessTierStats = containerStats.BlobsStatistics[StandardBlobTier.Cool]; coolAccessTierStats.Count += 1; coolAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingCoolAccessTierStats = containerStats.MatchingBlobsStatistics[StandardBlobTier.Cool]; matchingCoolAccessTierStats.Count += 1; matchingCoolAccessTierStats.Size += blobSize; matchingCoolAccessTierStats.BlobNames.Add(cloudBlockBlob.Name); } break; case StandardBlobTier.Archive: var archiveAccessTierStats = containerStats.BlobsStatistics[StandardBlobTier.Archive]; archiveAccessTierStats.Count += 1; archiveAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingArchiveAccessTierStats = containerStats.MatchingBlobsStatistics[StandardBlobTier.Archive]; matchingArchiveAccessTierStats.Count += 1; matchingArchiveAccessTierStats.Size += blobSize; matchingArchiveAccessTierStats.BlobNames.Add(cloudBlockBlob.Name); } break; } } } }while (token != null); } catch (Exception exception) { } return(containerStats); }
/// <summary> /// Checks the blob to see if it can be archived. /// </summary> /// <param name="blob"></param> /// <param name="filterCriteria"></param> /// <returns></returns> private static async Task <bool> DoesBlobMatchFilterCriteria(BlobClient blob, Models.FilterCriteria filterCriteria) { BlobProperties blobProperties = await blob.GetPropertiesAsync(); if (blobProperties.AccessTier == AccessTier.Archive) { return(false); } var dateTimeFrom = filterCriteria.LastModifiedDateFrom ?? DateTime.MinValue; var dateTimeTo = filterCriteria.LastModifiedDateTo ?? DateTime.MaxValue; var minBlobSize = filterCriteria.MinBlobSize; bool isDateTimeCheckPassed = false; if (blobProperties.LastModified != null) { var lastModified = blobProperties.LastModified.DateTime; if (dateTimeFrom <= lastModified && dateTimeTo >= lastModified) { isDateTimeCheckPassed = true; } } bool isBlobSizeCheckPassed = blobProperties.ContentLength >= minBlobSize; return(isDateTimeCheckPassed || isBlobSizeCheckPassed); }
/// <summary> /// Analyzes blobs in a container. This method will list all blobs in a blob container and /// matches them against the filter criteria. /// </summary> /// <param name="containerName">Name of the blob container.</param> /// <param name="filterCriteria"><see cref="Models.FilterCriteria"/></param> /// <returns> /// <see cref="Models.ContainerStatistics"/> which contains information about block blobs /// in different tiers and puts them in 2 buckets - all block blobs & block blobs that match /// the filter criteria. /// </returns> public static async Task <Models.ContainerStatistics> AnalyzeContainer(string containerName, Models.FilterCriteria filterCriteria) { var blobContainer = s_blobServiceClient.GetBlobContainerClient(containerName); var containerStats = new Models.ContainerStatistics(containerName); await foreach (var blob in blobContainer.GetBlobsAsync()) { if (blob != null) { long blobSize = blob.Properties.ContentLength.GetValueOrDefault(); DateTime blobLastModifiedDate = blob.Properties.LastModified.Value.DateTime; var doesBlobMatchFilterCriteria = await DoesBlobMatchFilterCriteria(blobContainer.GetBlobClient(blob.Name), filterCriteria); var blobTier = blob.Properties.AccessTier; switch (blobTier.Value.ToString()) { case null: case "Hot": var hotAccessTierStats = containerStats.BlobsStatistics[AccessTier.Hot]; hotAccessTierStats.Count += 1; hotAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingHotAccessTierStats = containerStats.MatchingBlobsStatistics[AccessTier.Hot]; matchingHotAccessTierStats.Count += 1; matchingHotAccessTierStats.Size += blobSize; matchingHotAccessTierStats.BlobNames.Add(blob.Name); } break; case "Cool": var coolAccessTierStats = containerStats.BlobsStatistics[AccessTier.Cool]; coolAccessTierStats.Count += 1; coolAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingCoolAccessTierStats = containerStats.MatchingBlobsStatistics[AccessTier.Cool]; matchingCoolAccessTierStats.Count += 1; matchingCoolAccessTierStats.Size += blobSize; matchingCoolAccessTierStats.BlobNames.Add(blob.Name); } break; case "Archive": var archiveAccessTierStats = containerStats.BlobsStatistics[AccessTier.Archive]; archiveAccessTierStats.Count += 1; archiveAccessTierStats.Size += blobSize; if (doesBlobMatchFilterCriteria) { var matchingArchiveAccessTierStats = containerStats.MatchingBlobsStatistics[AccessTier.Archive]; matchingArchiveAccessTierStats.Count += 1; matchingArchiveAccessTierStats.Size += blobSize; matchingArchiveAccessTierStats.BlobNames.Add(blob.Name); } break; } } } return(containerStats); }