0

Im trying to delete an Azure blob container of folder structure:

MyStorageAccount > MyContainer > MySubfolder > List of Blobs

I want to delete the subfolder including all blobs inside it MySubfolder > List of Blobs

The Uri:

*********** blobUri:https://myapp.blob.core.windows.net/containerID/subFolderID

containerID and subfolderID are correct, I have checked them in the Azure Portal.

I am getting this error when debugging. Why is my Uri invalid?

Exception thrown: 'Azure.RequestFailedException' in System.Private.CoreLib.dll
HTTP error code 400: InvalidUri
The requested URI does not represent any resource on the server.
RequestId:1af585de-1234-00yy-0ce1-50xxxx000000
Time:2022-04-15T15:56:47.0801054Z
Status: 400 (The requested URI does not represent any resource on the server.)
ErrorCode: InvalidUri

My DeleteBlobs class

using Azure;
using Azure.Storage;
using Azure.Storage.Blobs;
using MyApp.Data;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace MyApp.Areas.FunctionalLogic
{
    public class DeleteBlobs
    {
        private readonly DBConnectionStringFactory _DBConnectionStringFactory = new();

        public async Task<bool> DeleteAzureBlobAsync(string containerID, string subFolderID)
        {
            string connectionString = getConnectionString();
            var blobUri = getBlobUri(containerID, subFolderID).ToString();
            Debug.WriteLine("*********** blobUri:" + blobUri);
            BlobServiceClient container = new BlobServiceClient(connectionString);

            try
            {
                await container.DeleteBlobContainerAsync(blobUri);
            }
            catch (RequestFailedException e)
            {
                Debug.WriteLine("HTTP error code {0}: {1}", e.Status, e.ErrorCode);
                Debug.WriteLine(e.Message);
            }
            return true;
        }

        public string getConnectionString()
        {
            string connecString = Environment.GetEnvironmentVariable("MY_CONNECTION_STRING");
            return connecString;
        }

        public Uri getBlobUri(string containerName, string subFolderName)
        {
            Uri blobUri = new Uri("https://" + "myapp.blob.core.windows.net/" + containerName + "/" + subFolderName);

            return blobUri;
        }

    }
}

Update Answer:

   public async Task<bool> DeleteAzureBlobAsync(string containerID, string subFolderID)
    {
        string connectionString = getConnectionString();
        BlobContainerClient blobContainer = createContainerClient(connectionString, containerID);
        try
        {
            var resultSegment = blobContainer.GetBlobsAsync().AsPages();

            await foreach (Page<BlobItem> blobPage in resultSegment)
            {
                foreach (BlobItem blobItem in blobPage.Values)
                {
                    string blobName = blobItem.Name;
                    string processedName = blobName.Remove(blobName.LastIndexOf('/'));

                    if(processedName == subFolderID)
                    {
                        Debug.WriteLine("Delete: " + blobName);
                        await blobContainer.DeleteBlobAsync(blobName);
                    }
                }
            }
        }
        catch (RequestFailedException e)
        {
            Debug.WriteLine(e.Message);
            throw;
        }

        return true;
    }

    public BlobContainerClient createContainerClient(string connectionString, string containerName)
    {
        var containerClient = new BlobContainerClient(connectionString, containerName);
        return containerClient;
    }

2 Answers 2

1

You can’t, as folders don’t really exist in Azure blob storage; they’re just displayed as such to the user based on the blobs' names.

To delete the "folder", instead delete all the blobs in it (and sub-folders).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have updated the question with an implementation which seems to be working. Would you consider this 'ok' or is there a more efficient way to implement?
0

There's no sub folder in blob storage. You have Container, then all the rest is part of the blob name.

What you need to do is list the blobs based on prefix ('subfolder' name):

var blobs = blobContainer.ListBlobs(prefix = subFolderName, bool useFlatBlobListing = true);

then, for each match, delete the blob.

UPDATE:

The url may or may not be invalid, the issue is because you're passing the Uri when you should pass only blob name:

public virtual System.Threading.Tasks.Task<Azure.Response> DeleteBlobAsync (string blobName, Azure.Storage.Blobs.Models.DeleteSnapshotsOption snapshotsOption = Azure.Storage.Blobs.Models.DeleteSnapshotsOption.None, Azure.Storage.Blobs.Models.BlobRequestConditions conditions = default, System.Threading.CancellationToken cancellationToken = default);

source: https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobcontainerclient.deleteblobasync?view=azure-dotnet

1 Comment

Thanks, I have updated the question with an implementation which seems to be working. Would you consider this 'ok' or is there a more efficient way to implement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.