Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a blob using Azure Functions?

I am creating an Azure function that triggers when an image is uploaded or added to a particular Azure Storage, and it does the following: 1.) Resize the image 2.) Put the image to correct directory (using Output binding) 3.) Delete the original blob image that was added to Azure Storage after processing.

I am done with steps 1 and 2 in the process, but I'm finding less to no documentation about deleting a blob or an API that would expose methods for Azure Storage. (Using C#)

Here's the sample code:

#r "System.Drawing"
using System;
using ImageResizer;
using System.Drawing;
using System.Drawing.Imaging;

public static void Run(Stream inputImage, string imageName, Stream resizedImage, TraceWriter log)
{
    // Log the file name and size
    log.Info($"C# Blob trigger function Processed blob\n Name:{imageName} \n Size: {inputImage.Length} Bytes");

    // Manipulate the image
    var settings = new ImageResizer.ResizeSettings
    {
        MaxWidth = 400,
        Format = "png"
    };

    ImageResizer.ImageBuilder.Current.Build(inputImage, resizedImage, settings);

    // Delete the Raw Original Image Step
}
like image 986
Raven Avatar asked Feb 09 '17 06:02

Raven


1 Answers

To delete a blob, you need to

var container = blobClient.GetContainerReference(containerName);
var blockBlob = container.GetBlockBlobReference(fileName);
return blockBlob.DeleteIfExists();

Make sure that your close all streams before you try this so the image is no longer in use.

like image 132
albattran Avatar answered Sep 21 '22 11:09

albattran