Raven
Raven

Reputation: 2257

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
}

Upvotes: 13

Views: 15354

Answers (5)

zapoo
zapoo

Reputation: 1589

The following function is triggered with the file upload and deletes the file afterwards.

[FunctionName("MyFileTrigger")]
public async Task RunAsync(
  [BlobTrigger("csv-push/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
  [Blob("csv-push")] BlobContainerClient blobContainerClient,
  string name,
  ILogger log) {
  /// Some operations 

  if (response.IsSuccessful) {
    blobContainerClient.DeleteBlobIfExists(name);
  }
}

ps: Code maps the {name} with the file name you have uploaded to storage so please keep it as is.

Also documentation about it is here: https://learn.microsoft.com/bs-latn-ba/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=in-process%2Cextensionv5&pivots=programming-language-csharp

Upvotes: 1

Nattrass
Nattrass

Reputation: 1293

If you're using the latest library

Azure.Storage.Blobs

You can delete it like so...

BlobClient client = new BlobClient("connectionString", "container", "blobName");
client.DeleteIfExists();

Upvotes: 0

Tim P.
Tim P.

Reputation: 2942

Be sure to have the right references imported:

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;

Then you can use CloudBlockBlob as the parameter type and delete it:

public static void Run(CloudBlockBlob myBlob, string name, TraceWriter log)
{
    myBlob.DeleteIfExists();
}

Upvotes: 12

albattran
albattran

Reputation: 1907

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.

Upvotes: 12

Matt Mason
Matt Mason

Reputation: 2726

You can use several input types to your function when you use C#, here's the webjobs sdk cheat sheet detailing most of the available ones.

In your case, you could request your input image as a CloudBlockBlob, which has a delete method. You can call this inside the resizing function or in a separately triggered function to delete the completed blobs. You may need to change your binding direction to inout, see here.

There's no binding to do automatic cleanup at present.

Upvotes: 0

Related Questions