【发布时间】: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