node.js - 如何修复 azure blobs 身份验证错误授权标头/Mac 签名不匹配
问题描述
我正在使用 Azure Blob 并将图像作为 blob 上传到容器中,昨天一切正常,但今天由于某种原因,当我尝试创建 blob(上传图像)时出现错误:
(node:5412) UnhandledPromiseRejectionWarning: Error: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:742c79aa-601e-0046-2f47-bd0f64000000
Time:2019-02-05T11:38:04.9687033Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'iEGyntz0xOL6HddnoD/F6GxkHjyZlxEGTQ21OjlWCy4=' is not the same as any computed signature. Server used following string to sign: 'PUT
67
application/xml; charset=utf-8
我已经创建了容器,它具有容器和 blob 访问集,这就是它以前的工作方式。我取回一个 containerURL 和一个 blobURL,就好像该 blob 已创建一样我取回了容器 URL:
https://facerstorage.blob.core.windows.net/suspects
我还得到了 BlobURL:
"https://facerstorage.blob.core.windows.net/suspects/whoever.jpg"
但是当我检查容器内的天蓝色仪表板时,blob 不存在。
我还检查了我的时间,我在 UTC+2 并将其切换到 UTC 0,这等于我从服务器获取的 GMT,但它昨天也在 GMT+2 工作。不知道为什么我会得到这个,以前一切正常。
谢谢
更新 1:
我将执行上传的代码放入 try catch 块中,但出现以下格式的错误:
'<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:6b27b7c2-601e-0024-474c-bdcd43000000\nTime:2019-02-05T12:14:44.3155159Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request \'n/0RjbOhVCOHSkWlq4JWqPsgMe7UxkuyrA1HV1Y1AcY=\' is not the same as any computed signature. Server used following string to sign: \'PUT\n\n\n67\n\napplication/xml; charset=utf-8\n\n\n\n\n\n\nx-ms-client-request-id:ce83a49c-51fb-476e-b4ab-25968c162d82\nx-ms-date:Tue, 05 Feb 2019 12:14:43 GMT\nx-ms-version:2018-03-28\n/facerstorage/watchlist/CHRISTOPHER%20ROBERT%20METSOS.jpg\ncomp:blocklist\ntimeout:60000\'.</AuthenticationErrorDetail></Error>',
headers: HttpHeaders { _headersMap: [Object] },
status: 403 },
body:
{ message:
'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:6b27b7c2-601e-0024-474c-bdcd43000000\nTime:2019-02-05T12:14:44.3155159Z' } }
这是我正在上传的代码,我从 azure sdk 中获取了基础并对其进行了一些修改:
async function uploadImage(aborter, containerURL, filePath, blobName) {
try{
filePath = path.resolve(filePath);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
console.log("Blob created, URL is: " + JSON.stringify(blockBlobURL.url));
const stream = fs.createReadStream(filePath, {
highWaterMark: FOUR_MEGABYTES,
});
const uploadOptions = {
bufferSize: FOUR_MEGABYTES,
maxBuffers: 5,
};
//SAVE URL TO DB
addToDB(blobName, blockBlobURL.url);
return await uploadStreamToBlockBlob(
aborter,
stream,
blockBlobURL,
uploadOptions.bufferSize,
uploadOptions.maxBuffers);
} catch (err) {console.log(err)}
}
更新#2
-检查了 Azure Keys 并刷新并替换了它们。
-能够制作容器而不会出现任何身份验证错误。
- 尝试使用常规 uploadLocalFile 而不是流也得到错误。
解决方案
发现错误,我上传的文件名称之间有空格,例如“Jhon Marcus”
尽管我已经使用以下方法处理了这个问题:
fileName = fileName.replace(/\s/gi, '-')
之前替换了空格,之后我在 bloname 中添加了一个类别以创建一个虚拟目录,并且该类别有空格,所以它是 /my cat/filename 并且是类别名称中的空格破坏了它。
但最后用 - 替换空格解决了我的问题。
感谢所有帮助过的人
推荐阅读
- c# - 无需构建的 Nuget 包提取
- json - 使用 wikipedia readinglist API 获得 100 多个结果
- sql - BigQuery:如何添加新的计算列?
- reactjs - React 17 上的 Page NotFound - 上下文 API 不出现
- git - 如果 `$file` 有未提交的更改,`git checkout $hash $file` 会出错吗?
- python - odoo 14 如何获得选定的记录
- sql - 从不相关的不同表中选择 2 个值并使用结果 BigQuery 执行操作
- javascript - React - 可以根据呈现的其他组件来设置组件的样式吗?
- c# - 从后台工作人员修改图表不起作用?
- reactjs - 如何扩展 Material-UI Button 并创建自己的按钮组件