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;
}