1

I am able to upload blobs containing cyrillic characters using Azure.Storage.Blobs 12.9.1 but then am unable to batch delete them using Azure.Storage.Blobs.Batch 12.6.0.

Consider the following blob name: здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp and code:

var blobsToBeDeleted = new List<Uri>();
blobsToBeDeleted.Add(new Uri("azure-storage-address/container/здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp"));
blobsToBeDeleted.Add(new Uri("azure-storage-address/container/thumbs/здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp"));
await batchClient.DeleteBlobsAsync(blobsToBeDeleted);

After running this against Azure the response is:

Azure.RequestFailedException
{"Invalid response\r\nStatus: 202 (Accepted)\r\n\r\nHeaders:\r\nTransfer-Encoding: chunked\r\nServer: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0\r\nx-ms-request-id: 9255671d-301e-008e-1a4a-797e59000000\r\nx-ms-version: 2020-08-04\r\nx-ms-client-request-id: ad39b21f-aad7-484e-8121-a707a318088e\r\nDate: Thu, 15 Jul 2021 07:24:29 GMT\r\nContent-Type: multipart/mixed; boundary=batchresponse_7c0d302a-8ee6-4d48-aac1-d973904338c3\r\n"}  Azure.RequestFailedException
{"Expected 2 responses for the batch request, not 1."}
Status 202

Other files having the name structure and using the same code as above, but containing only latin characters are deleted successfully.

new Uri() seems to properly escape characters and since the BlobBatchClient.DeleteBlobsAsync method accepts IEnumerable<Uri> I really don't understand where is the problem.

Any ideas on this?

EDIT

This is a know issue, already fixed and will be released in the next version - https://github.com/Azure/azure-sdk-for-net/issues/22329

2

1 Answer 1

2

Please try something like the following. It is a bit convoluted approach and the SDK can certainly be improved but it works:

private static string connectionString =
    "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key==";
private static string containerName = "container-name";

static async Task Main(string[] args)
{
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobBatchClient batchClient = new BlobBatchClient(blobServiceClient);
    
    List<string> blobNames = new List<string>()
    {
        "здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp",
        "thumbs/здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp"
    };
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    List<BlobClient> blobClients = blobNames.Select(b => containerClient.GetBlobClient(b)).ToList();
    List<Uri> blobsToBeDeleted = blobClients.Select(b => new Uri(Uri.EscapeUriString(b.Uri.AbsoluteUri))).ToList();
    await batchClient.DeleteBlobsAsync(blobsToBeDeleted);
}

UPDATE

So I had a need to implement this functionality in a project of mine and I found an alternate way to implement this functionality which is somewhat less convoluted. I thought I would share that as well here. Here's the code I ended up implementing:

private static string connectionString =
    "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key==";
private static string containerName = "container-name";
static async Task Main(string[] args)
{
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobBatchClient batchClient = new BlobBatchClient(blobServiceClient);
    
    List<string> blobNames = new List<string>()
    {
        HttpUtility.UrlEncode("здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.pdf"),
        HttpUtility.UrlEncode("здраве-и-красота-от-природатаjpg--xfgqfvhj.pdf")
    };
    BlobBatch batch = new BlobBatch(batchClient);
    foreach (var blobName in blobNames)
    {
        batch.DeleteBlob(containerName, blobName, DeleteSnapshotsOption.IncludeSnapshots);
    }

    await batchClient.SubmitBatchAsync(batch);
}
4
  • Just tested and confirming this approach works fine. It's really a bit overwhelming but does the job. Other solution would be to use the regular DeleteBlobAsync() method which accepts a regular string and works as expected.
    – Ivaylo
    Commented Jul 15, 2021 at 10:47
  • It's really a bit overwhelming - That's an understatement :). SDK can most certainly be improved considering the REST API only expects the partial path (container name + blob name). Commented Jul 15, 2021 at 10:55
  • 1
    I really appreciate your answer because this was causing issues for a while in our application. And I am impressed nobody opened an issue for this since now :)
    – Ivaylo
    Commented Jul 15, 2021 at 11:04
  • 1
    @IvayloDimitrov - Updated my answer with somewhat cleaner approach. HTH. Commented Jul 18, 2021 at 4:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.