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

Node.js调用Azure REST API复制归档层Blob的PUT请求如何修复

修复方案

你需要修改以下4处问题即可正常运行:

1. 修复基础语法错误

  • 你定义blobUrl和调用fetch时的URL都缺失了模板字符串的反引号,修改为:
const blobUrl = `https://${account}.blob.core.windows.net/${containerName}/${blobName}`
// fetch调用修改为
fetch(`https://${account}.blob.core.windows.net/${containerName}/${blobName}`, putConfig)

2. 补全签名字符串的x-ms自定义头

Azure Shared Key签名要求所有以x-ms-开头的请求头都必须按字典序小写排序后加入签名字符串,你新增的x-ms-copy-source、x-ms-requires-sync没有加入签名,会触发403鉴权失败。
同时PUT请求的Content-Length对应位置需要填0(因为同步复制Blob不需要请求体),修改后的签名字符串为:

const strToSign = `PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-copy-source:${blobUrl}\nx-ms-date:${strTime}\nx-ms-requires-sync:true\nx-ms-version:2020-10-02\n/${account}/${containerName}/${blobName}`;

注意:这里按字典序排列x-ms头的顺序是:x-ms-copy-source → x-ms-date → x-ms-requires-sync → x-ms-version,顺序错误也会导致签名失败。

3. 给PUT请求补全Content-Length请求头

同步复制Blob的PUT请求不需要请求体,必须显式指定Content-Length: 0,否则会触发411长度必需错误,修改putConfig如下:

const putConfig = {
    method: 'PUT', 
    headers: {
        'Authorization': auth,
        'x-ms-date': strTime,
        'x-ms-version': "2020-10-02",
        'x-ms-copy-source': blobUrl,
        'x-ms-requires-sync':'true',
        'Content-Length': '0'
    }
}

4. 处理fetch的异步逻辑错误

你原来的代码中res.end()没有等fetch请求完成就执行了,会导致请求提前中断,修改异步逻辑:

export default async (req, res) => {
    if (req.method === 'POST') {
        try {
            const results = await fetch(`https://${account}.blob.core.windows.net/${containerName}/${blobName}`, putConfig)
            if(results.status==202) {
                console.log('复制请求已提交')
            } else {
                console.log(await results.text())
            }
        } catch (e) {
            console.error(e)
        } finally {
            res.end()
        }
    } 
}

注意:归档层Blob同步复制成功会返回202状态码,不是200。

内容的提问来源于stack exchange,提问作者Andres R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 11:45:06