Introduction
This is my fourth article in the series of Dynamics 365 and Azure function integration. In earlier articles, we discussed the integration of Dynamics 365 with Azure, using hard-coded credentials and using the server to server authentication. Now, we are going to discuss how we can delete Azure blob after it is processed. You can refer to my earlier articles from the following links.
Details
Once we have processed our blob storage (
if you are new to blob storage, you can refer to this KB), let’s say we want to delete it after our Azure Functions trigger has completed execution. We can delete it using the delete method from CloudBlockBlob class. To delete blob storage, we need the following 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 Functions trigger, so we can pass the same.
ConnectionString
This is the connection string for the Azure blob storage. We can get it while navigating to integrate from our Azure Functions 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 Functions" trigger. We need to use the string before the forward slash (/), like the following.
Now, we can use these details in the following code to delete the blob storage.
- public static void DeleteFromBlob(string filename, string connectionString, string containerName, TraceWriter log) {
- try {
-
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
-
-
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
-
-
- CloudBlobContainer container = blobClient.GetContainerReference(containerName);
-
-
- CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
-
-
- blockBlob.Delete();
- } catch (Exception e) {
- log.Info($ "Error While delete blob storage {e.Message}");
- }
- }
Keep in mind that to use the above methods, we need to include the following reference in our Azure Functions trigger.
- #r "Microsoft.WindowsAzure.StorageClient"
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.StorageClient;
Stay tuned for more Dynamics 365 content!