You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Azure Blob Storage REST API批量删除文件报错InvalidInput如何解决

报错根因

以下是触发InvalidInput错误的几个核心问题:

  • 子请求Host头格式错误:子请求的Host字段仅需填写域名,不能携带https://协议前缀,你代码中该字段填写为https://<accountName>.blob.core.windows.net不符合HTTP规范。
  • 子请求多余携带授权信息:Blob Batch接口的授权仅需在外层请求头携带,子请求内部重复添加Authorization、x-ms-date头会触发校验失败。
  • 版本号不一致:代码中声明外层x-ms-version为2020-04-08,但实际生成的请求中该值为2018-11-09,版本不匹配会被判定为无效输入。
  • 分隔符换行不规范:所有批次分隔符前后必须严格使用\r\n换行,你代码中separation变量使用了\n,格式不符合要求。
  • 多请求拼接逻辑缺失:你需要批量删除两个文件,但当前代码仅构造了一个删除子请求,没有遍历fileList拼接多个子请求内容。
修复方案

按以下规则调整代码即可正常实现批量删除:

  1. 移除子请求中的Authorization、x-ms-date头,修正Host字段为纯域名
  2. 统一内外层x-ms-version版本,推荐使用2020-04-08及以上版本
  3. 遍历文件列表拼接多个删除子请求,每个子请求之间用批次分隔符分隔
  4. 严格统一所有换行符为\r\n,确保Content-Length计算准确

修正后的代码示例:

const boundary = "batch_357de4f7-6d0b-4e02-8cd2-6361411a9525";
const separator = `--${boundary}\r\n`;
const ending = `--${boundary}--\r\n`;
const accountName = "<你的存储账户名>";
const containerName = "<你的容器名>";
const fileList = ['sample1.json','sample2.json'];
const strTime = new Date().toUTCString();
const authToken = "<你的授权token>";

// 拼接所有子请求
let subRequestContent = '';
fileList.forEach(fileName => {
  subRequestContent += separator;
  subRequestContent += 'Content-Type: application/http\r\n';
  subRequestContent += 'Content-Transfer-Encoding: binary\r\n\r\n';
  subRequestContent += `DELETE /${containerName}/${fileName} HTTP/1.1\r\n`;
  subRequestContent += `Host: ${accountName}.blob.core.windows.net\r\n`;
  subRequestContent += 'Content-Length: 0\r\n\r\n';
});
subRequestContent += ending;

const requestObj = {
  url: `https://${accountName}.blob.core.windows.net/?comp=batch`,
  method: 'POST',
  headers: {
    'Content-Type': `multipart/mixed; boundary=${boundary}`,
    "x-ms-date": strTime,
    "x-ms-version": "2020-04-08",
    "Content-Length": subRequestContent.length,
    Authorization: `Bearer ${authToken}`
  },
  body: subRequestContent
}

request(requestObj, (err, resp) => {
  // 业务逻辑
})

内容的提问来源于stack exchange,提问作者Akshay Vashishtha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 04:54:04