Introduction
This is our fourth article in the series of Dynamics 365 and Azure function integration. In earlier articles, we discuss how we integration Dynamics 365 with Azure, using hard-coded credentials and using the server to server authentication. Now we are going to discuss how can we delete Azure blob after it is processed. You can refer our earlier articles from following links
Dynamics 365 CE and Azure Function Part 1
Dynamics 365 CE and Azure Function Part 2
Dynamics 365 CE and Azure Function Part 3
Details
Once we have processed our blob storage (if you are new to blob storage, you can refer this KB), let’s say we want to delete it after our Azure function trigger completed execution, we can delete it using delete method from CloudBlockBlob class. To delete blob storage we need three things
Filename
This is the name of the blob storage file that we want to delete. We can get this parameter in our Run method in Azure function trigger, so we can pass the same.
ConnectionString
This is the connection string for the Azure blob storage, we can get it from navigating to Integrate from our Azure function trigger like following
ContainerName
This is the name of the container which holds our blob storage, we can also get this by navigating to Integrate from our Azure function trigger. We need to use string before the / like following.
Now we can use these details in following code to delete blob storage.
public static void DeleteFromBlob(string filename, string connectionString, string containerName, TraceWriter log) { try { // Retrieve storage account from connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference(containerName); // Retrieve reference to a blob named "myblob.csv". CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename); // Delete the blob. blockBlob.Delete(); } catch (Exception e) { log.Info($ "Error While delete blob storage {e.Message}"); } }
Keep in mind to use the above methods we need to include following reference in our Azure function trigger.
#r "Microsoft.WindowsAzure.StorageClient" using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient;
Stay tuned for more Dynamics 365 contents !!
Pingback: Dynamics 365 CE and Azure Function Part 4 - Microsoft Dynamics CRM Community