Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files older than X number of days from Azure Blob Storage using Azure function

I want to create an Azure function that deletes files from azure blob storage when last modifed older than 30 days. Can anyone help or have a documentation to do that?

like image 695
Dani Avatar asked Jul 05 '19 13:07

Dani


3 Answers

Assuming your storage account's type is either General Purpose v2 (GPv2) or Blob Storage, you actually don't have to do anything by yourself. Azure Storage can do this for you.

You'll use Blob Lifecycle Management and define a policy there to delete blobs if they are older than 30 days and Azure Storage will take care of deletion for you.

You can learn more about it here: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts.

like image 120
Gaurav Mantri Avatar answered Oct 19 '22 06:10

Gaurav Mantri


You can create a Timer Trigger function, fetch the list of items from the Blob Container and delete the files which does not match your criteria of last modified date.

  1. Create a Timer Trigger function.
  2. Fetch the list of blobs using CloudBlobContainer.
  3. Cast the blob items to proper type and check LastModified property.
  4. Delete the blob which doesn't match criteria.

I hope that answers the question.

like image 36
Nouman Avatar answered Oct 19 '22 06:10

Nouman


I have used HTTP as the trigger as you didn't specify one and it's easier to test but the logic would be the same for a Timer trigger etc. Also assumed C#:

[FunctionName("HttpTriggeredFunction")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [Blob("sandbox", Connection = "StorageConnectionString")] CloudBlobContainer container,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    // Get a list of all blobs in your container
    BlobResultSegment result = await container.ListBlobsSegmentedAsync(null);

    // Iterate each blob
    foreach (IListBlobItem item in result.Results)
    {
        // cast item to CloudBlockBlob to enable access to .Properties
        CloudBlockBlob blob = (CloudBlockBlob)item;

        // Calculate when LastModified is compared to today
        TimeSpan? diff = DateTime.Today - blob.Properties.LastModified;
        if (diff?.Days > 30)
        {
            // Delete as necessary
            await blob.DeleteAsync();
        }
    }

    return new OkObjectResult(null);
}

Edit - How to download JSON file and deserialize to object using Newtonsoft.Json:

public class MyClass
{
    public string Name { get; set; }
}

var json = await blob.DownloadTextAsync();
var myClass = JsonConvert.DeserializeObject<MyClass>(json);
like image 37
Chris B Avatar answered Oct 19 '22 06:10

Chris B