Created
December 28, 2012 16:31
-
-
Save anlai/4399376 to your computer and use it in GitHub Desktop.
Function that deletes files out of an azure storage container, after X amount of days.
This file contains hidden or 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
private readonly string _storageAccountName; | |
private readonly string _storageKey; | |
private readonly string _storageContainer; | |
private readonly int _cleanupThreshold; | |
private const string CloudStorageconnectionString = @"DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}"; | |
public IEnumerable<string> BlobCleanup() | |
{ | |
var storageAccount = CloudStorageAccount.Parse(string.Format(CloudStorageconnectionString, _storageAccountName, _storageKey)); | |
var client = storageAccount.CreateCloudBlobClient(); | |
var container = client.GetContainerReference(_storageContainer); | |
var blobs = container.ListBlobs(null, true); | |
var filtered = blobs.Where(a => a is CloudBlockBlob && | |
((CloudBlockBlob)a).Properties.LastModified < DateTime.Now.AddDays(_cleanupThreshold) | |
).ToList(); | |
var deleted = new List<string>(); | |
foreach(var item in filtered) | |
{ | |
var blob = (CloudBlockBlob)item; | |
deleted.Add(blob.Name); | |
blob.Delete(); | |
} | |
return deleted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment