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

偶尔善良 提交于 2020-06-12 04:31:21

问题


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?


回答1:


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://docs.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts.




回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/56903837/delete-files-older-than-x-number-of-days-from-azure-blob-storage-using-azure-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!