Created
July 31, 2017 06:52
-
-
Save brianweet/74c207c82eb8613960357e0b756ef1b7 to your computer and use it in GitHub Desktop.
Cleanup azure blobs epi (UNTESTED!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using EPiServer.PlugIn; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Web; | |
using EPiServer.Framework.Blobs; | |
using EPiServer.Azure.Blobs; | |
namespace DockerEpiserverSite1 | |
{ | |
// Based on https://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=155137 | |
[ScheduledPlugIn(DisplayName = "CleanUpAzureBlobsJob", Description = "This job iterates all blobs in a given Azure container and removes the ones that do not exists in CMS")] | |
public class CleanUpAzureBlobsJob : EPiServer.Scheduler.ScheduledJobBase | |
{ | |
private static Injected<IBlobProviderRegistry> InjectedBlobFactory { get; set; } | |
private readonly IBlobProviderRegistry _blobProviderRegistery = InjectedBlobFactory.Service; | |
private static Injected<IPermanentLinkMapper> InjectedPermanentLinkMapper { get; set; } | |
private readonly IPermanentLinkMapper _permanentLinkMapper = InjectedPermanentLinkMapper.Service; | |
public override string Execute() | |
{ | |
// Get current blob provider | |
var provider = GetBlobProvider() as AzureBlobProvider; | |
if (provider == null) | |
{ | |
throw new NotImplementedException(); | |
} | |
// Get storage account credentials | |
var containerName = ; | |
var connectionString = ConfigurationManager.ConnectionStrings["EPiServerAzureBlobs"].ConnectionString; | |
var container = provider.ContainerFactory.GetOrCreateContainer(connectionString, containerName); | |
// Get all blobs in the given container | |
var blobs = container.GetBlobs(string.Empty); | |
// Check which blobs to remove | |
var blobsTobeRemoved = GetBlobsToRemove(blobs); | |
// Remove blobs | |
var success = 0; | |
var failed = 0; | |
OnStatusChanged($"Start removing {blobsTobeRemoved.Count()} blobs"); | |
foreach (var azureBlob in blobsTobeRemoved) | |
{ | |
try | |
{ | |
azureBlob.DeleteIfExists(); | |
success++; | |
} | |
catch | |
{ | |
failed++; | |
} | |
} | |
return $"Removed blobs: {success}. Failed to remove: {failed}"; | |
} | |
private BlobProvider GetBlobProvider() | |
{ | |
var containerIdentifier = Blob.GetContainerIdentifier(Guid.NewGuid()); | |
var provider = _blobProviderRegistery.GetProvider(containerIdentifier); | |
return provider; | |
} | |
private string GetDirectoryNameFromBlob(AzureBlob blob) | |
{ | |
return blob.ID.Segments.ElementAt(2).Replace("/", string.Empty); | |
} | |
private List<AzureBlob> GetBlobsToRemove(IEnumerable<AzureBlob> blobs) | |
{ | |
var blobsToRemove = new List<AzureBlob>(); | |
var count = 1; | |
foreach (var blob in blobs) | |
{ | |
OnStatusChanged($"Checking blob #{count}"); | |
var guid = Guid.Parse(GetDirectoryNameFromBlob(blob)); | |
var map = _permanentLinkMapper.Find(guid); | |
if (null == map) | |
{ | |
blobsToRemove.Add(blob); | |
} | |
count++; | |
} | |
return blobsToRemove; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment