【问题标题】:Azure REST API PUT Header in Node.js for Copying BlobNode.js 中用于复制 Blob 的 Azure REST API PUT 标头
【发布时间】:2021-10-27 22:41:24
【问题描述】:

运行以下 GET 请求 node.js 有效:

const CryptoJS = require("crypto-js");
const account = 'account'
const containerName = 'container'
const blobName = 'picture.jpeg'
const blobUrl = `https://${account}.blob.core.windows.net/${containerName}/${blobName}`

const key = 'accesskey'
const strTime = (new Date()).toUTCString();
const strToSign = `GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:${strTime}\nx-ms-version:2020-10-02\n/${account}/\ncomp:list`;

const secret = CryptoJS.enc.Base64.parse(key);
const hash = CryptoJS.HmacSHA256(strToSign, secret);
const hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
const auth = `SharedKey ${account}:`+hashInBase64; 

let getConfig = {
    headers: {
        'Authorization': auth,
        'x-ms-date': strTime,
        'x-ms-version': "2020-10-02",
    }
}

   
export default async (req, res) => {
    if (req.method === 'POST') {

        fetch(`https://${account}.blob.core.windows.net/?comp=list`, getConfig)
        .then( results => {
            if(results.status==200) {console.log('api works')} else {console.log(results)}},
            res.end()
        )
    } 
}

然后我想复制一个访问层为“存档”的 blob。对 PUT 请求运行具有以下更改的相同代码不起作用:

const blobUrl = https://${account}.blob.core.windows.net/${containerName}/${blobName}

const strToSign = PUT\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:${strTime}\nx-ms-version:2020-10-02\n/${account}/\n${containerName}/\n${blobName};

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',
    }
}
    

fetch(https://${account}.blob.core.windows.net/${containerName}/${blobName}, putConfig)

谁能告诉我需要更改什么才能成功运行 PUT 请求?

【问题讨论】:

    标签: node.js azure sdk header put


    【解决方案1】:

    您需要在第二个strToSign 中添加x-ms-copy-sourcex-ms-requires-sync 请求标头。

    基本上,您需要在规范化的标头字符串中包含所有以x-ms 开头的标头。

    您还需要确保这些标题按字母顺序排序。

    所以你的strToSign 会是这样的:

    const strToSign = PUT\n\n\n\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}/\n${containerName}/\n${blobName}
    

    blobUrl 是您要复制的源 blob 的 URL。

    更多详情请查看此链接:https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key#constructing-the-canonicalized-headers-string

    顺便说一句,你看过Azure Blob SDK for JavaScript吗?

    【讨论】:

    • 感谢您发送此信息。我检查了blobUrl,我按照你说的做了。我仍然得到:403错误。 '服务器未能验证请求。确保.. 复制并粘贴您的 strToSign // Header const putConfig = { method: 'PUT', headers: { 'Authorization': auth, 'x-ms-copy-source': blobUrl, 'x-ms-date' : strTime, 'x-ms-requires-sync':'true', 'x-ms-version': "2020-10-02" } }
    • 关于 SDK:我试过了,但 SDK 不允许对层访问状态为“存档”的 blob 执行“从 url 复制 blob”操作。这就是我需要的。否则,是的,我能够使用 SDK 复制“热”或“冷”访问层 blob。
    【解决方案2】:

    您需要在 putConfig 的标头中提供必需的属性。

    所以,我明白了,此时缺少两个必需的属性。 x-ms-lease-id:<ID>Content-Length: 0 对于页 blob 或附加 blob,此标头的 Content-Length 值必须设置为零,因为 Put Blob 仅用于初始化 blob。

    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',
            'x-ms-lease-id': <ID>,
            'Content-Length': 0,
        }
    }

    在getConfig中,你使用了x-ms-version上面的2012-02-12。因此,在 2012-02-12 及更高版本中,x-ms-lease-id:&lt;ID&gt; 值必须为租用的 blob 指定活动的无限租用。有限期限租约 ID 失败并显示 412(前提条件失败)。

    更多详情请查看此链接:https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob

    因此,您需要像这样更新 getConfig:-

    let getConfig = {
        headers: {
            'Authorization': auth,
            'x-ms-date': strTime,
            'x-ms-version': "2020-10-02",
            'x-ms-lease-id': <ID>,
        }
    }

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 2021-10-30
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2013-01-08
    相关资源
    最近更新 更多