Is it possible to delete the blob which triggers an azure function

Peter Sharp 181 Reputation points
2021-11-03T03:24:51.783+00:00

I've read around and it seems like people are doing this, but I just can't seem to get it to work.

how-to-delete-a-blob-using-azure-functions

cannot-delete-the-blob-that-triggers-an-azure-function-blob-input-binding

Can anyone provide me with guidance on having a blob trigger for a function, read that blob as a stream, and when finished, delete the triggering blob?

Many thanks

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,910 questions
0 comments No comments
{count} votes

Accepted answer
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2021-11-03T11:17:22.79+00:00

    @Peter Sharp ,

    Thanks for reaching out to Q&A.

    Yes, it appears that the deletion of the blob is possible via azure functions. You can read the blob as a stream by referring to this article : https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=csharp

    You can delete the blob using the below code. You will have to add the nuget package namespace "using Azure.Storage.Blobs"

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

    You might also find the blob receipts to be interesting.

    I hope this helps!

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Sharp 181 Reputation points
    2021-11-08T06:59:37.887+00:00

    In case it helps anyone, here is how I did it.

    Develop locally using Visual Studio (or alternative if you can make it work). The instructions in 'Function App > Overview > Quickstart > Start with a code editor' work.

    Use Nuget Package Manager (Tools > Nuget Package Manager > Manage NuGet Packages for Solution) and update 'Microsoft.Azure.WebJobs.Storage' to version 5.0.0.

    This has Azure.Storage.Blobs as a dependency - and this version doesn't have the bug when prevents the initial version from building properly.

    Leave Microsoft.NET.Sdk.Functions at 3.0.13 to stay with LTS version of .NET.

    Then ...

    using Azure.Storage.Blobs;
    

    ... and then ...

    var storageConnectionString = System.Environment.GetEnvironmentVariable("AzureWebJobsStorage");
    

    This loads the connection string from Application Settings.

    From there, the following should just work.

                    var client = new BlobClient(storageConnectionString, "test-path", "in/" + name);
                    client.DeleteIfExists();
    

    Hope this helps someone.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.