Azure blob 批量删除无法使用 rest API 删除多个文件

Posted

技术标签:

【中文标题】Azure blob 批量删除无法使用 rest API 删除多个文件【英文标题】:Azure blob batch deletion failing to delete multiple file using rest API 【发布时间】:2022-01-02 20:35:01 【问题描述】:

我尝试通过 REST API 在 Azure blob 中进行批量删除。我已经创建了子请求

const test = `--batch_357de4f7-6d0b-4e02-8cd2-6361411a9525\r
Content-Type: application/http\r
Content-Transfer-Encoding: binary\r
\r
DELETE https://<accountName>.blob.core.windows.net/$azureDirectoryName/$blobName HTTP/1.1\r
Host: https://<accountName>.blob.core.windows.net\r
x-ms-date: $strTime\r
Authorization: Bearer $authToken\r
Content-Length: 0\r
--batch_357de4f7-6d0b-4e02-8cd2-6361411a9525--\r`

我在 nodeJs 中的请求对象是


   "url":"https://<accountName>.blob.core.windows.net/?comp=batch",
   "method":"POST",
   "headers":
      "Content-Type":"multipart/mixed; boundary=END_OF_PART",
      "x-ms-date":"Wed, 24 Nov 2021 12:35:05 GMT",
      "x-ms-version":"2020-04-08",
      "Content-Length":10411,
      "Authorization":"Bearer <token>"
   ,
   "body":"--END_OF_PART\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\n\r\nDELETE https://<account>.blob.core.windows.net/container/blob.txt\r\nx-ms-date: Wed, 24 Nov 2021 12:35:05 GMT\r\nAuthorization: Bearer <token>\r\nContent-Length: 0\r\n\r\n--END_OF_PART\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\n\r\nDELETE https://<accountName>.blob.core.windows.net/container/blob.txt\r\nx-ms-date: Wed, 24 Nov 2021 12:35:05 GMT\r\nAuthorization: Bearer <token>\r\nContent-Length: 0\r\n\r\n--END_OF_PART\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\n\r\nDELETE https://<accountName>.blob.core.windows.net/container/blob.txt\r\nx-ms-date: Wed, 24 Nov 2021 12:35:05 GMT\r\nAuthorization: Bearer <token>\r\nContent-Length: 0\r\n\r\n--END_OF_PART\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\n\r\nDELETE https://<accountName>.blob.core.windows.net/container/blob.txt\r\nx-ms-date: Wed, 24 Nov 2021 12:35:05 GMT\r\nAuthorization: Bearer <token>\r\nContent-Length: 0\r\n\r\n--END_OF_PART\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\n\r\nDELETE https://<accountName>.blob.core.windows.net/container/blob.txt\r\nx-ms-date: Wed, 24 Nov 2021 12:35:05 GMT\r\nAuthorization: Bearer <token>\r\nContent-Length: 0\r\n\r\n--END_OF_PART--"

我收到以下错误:

'--batchresponse_144e6c6b-2bbc-4f52-900e-d213906fac30
Content-Type: application/http

HTTP/1.1 400 One of the request inputs is not valid.
x-ms-error-code: InvalidInput
x-ms-request-id: requestId
x-ms-version: 2020-04-08
Content-Length: 221
Content-Type: application/xml
Server: Windows-Azure-Blob/1.0

<?xml version="1.0" encoding="utf-8"?>
<Error><Code>InvalidInput</Code><Message>One of the request inputs is not valid.
RequestId:requestId
Time:2021-11-24T12:46:45.4660320Z</Message></Error>
--batchresponse_144e6c6b-2bbc-4f52-900e-d213906fac30--'

【问题讨论】:

您的子请求应该有主机名,它应该只有部分 URL。请参阅documentation 重新构建您的 http 请求正文。 这个基础部分 'https://.blob.core.windows.net/ 不应出现在您的子请求中。 【参考方案1】:

谢谢Anand Sowmithiran。发布您的建议作为帮助其他社区成员的答案。

您的子请求不应包含主机名,而应仅包含部分 URL。

https://&lt;account&gt;.blob.core.windows.net/ 这个基础部分不应出现在您的sub-requests

可以参考Blob Batch - Request Body

【讨论】:

以上是关于Azure blob 批量删除无法使用 rest API 删除多个文件的主要内容,如果未能解决你的问题,请参考以下文章

从 Azure 存储 Blob 中批量删除“x”天前的对象

为 Azure Blob 存储批量更改层

使用服务帐户从 Azure Blob 存储中批量删除文件

从 BLOB 批量插入 Azure SQL 说文件无法打开

Azure 搜索服务 REST API 删除错误:“文档密钥不能丢失或为空。”

Azure blob 到 Azure SQL 数据库:无法批量加载,因为无法打开文件“xxxx.csv”。操作系统错误代码 5(访问被拒绝。)