Azure blob 批量删除西里尔字符

Azure blob batch delete cyrillic characters

我可以使用 Azure.Storage.Blobs 12.9.1 上传包含西里尔字符的 blob,但无法使用 Azure.Storage.Blobs.Batch 12.6.0.

批量删除它们

考虑以下 blob 名称:здраве-и-красота-от-природатаjpg--xfgqfvhj.axp.webp 和代码:

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

在 运行 之后针对 Azure 的响应是:

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

其他具有与上述名称结构且使用相同代码但仅包含拉丁字符的文件已成功删除。

new Uri() 似乎正确地转义字符,并且由于 BlobBatchClient.DeleteBlobsAsync 方法接受 IEnumerable<Uri> 我真的不明白问题出在哪里。

对此有什么想法吗?

编辑

这是一个已知问题,已经修复并将在下一版本中发布 - https://github.com/Azure/azure-sdk-for-net/issues/22329

请尝试以下操作。这是一个有点复杂的方法,SDK 当然可以改进,但它有效:

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

更新

所以我需要在我的一个项目中实现这个功能,我找到了一种替代方法来实现这个功能,这种方法不太复杂。我想我也会在这里分享。这是我最终实现的代码:

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