Search code examples
azure-blob-storagesnapshot

Automatically trigger the snapshot when file has modified in blob storage


I use Azure blob storage to back data.The back up is done by Azure Data Factory that extract from the CosmosDB. The structure for store the backed up data is: Data Storage Account --> Container --> the JSON files I saved daily. However, the back up is just overwrites and I won't be able to download the previous data.

I realized there is a Create Snapshot button and when I select the JSON files(the Blobs) and clicked the button, I can get the snapshot of the JSON files, how can I generate the Snapshots automatically when the time stamp has changed on those JSON files? In another word once the data factory packed today's data the Snapshots for today will then be created.


Solution

  • You can create an azure function with blob trigger. If the file is modified, the function will be executed, and you should put the snapshot logic in the function.

    Here is the sample code(Note: for the first parameter myBlob, change the type to CloudBlockBlob):

    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("images/{name}", Connection = "AzureWebJobsStorage")]CloudBlockBlob myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name}");
            log.LogInformation("take snapshots for blob: " + name);
            myBlob.SnapshotAsync().Wait();
        }
    }
    

    and here is the settings in the local.settings.json:

    {
        "IsEncrypted": false,
        "Values": {
            "AzureWebJobsStorage": "connection string of the azure storage account",
            "FUNCTIONS_WORKER_RUNTIME": "dotnet"
        }
    }