【发布时间】:2021-06-06 01:52:41
【问题描述】:
我能够CreateContainers、ListContainers、ListBlobs,但是当我尝试向上传或删除文件发出PUT/DELETE请求时在 Azure 存储 blob 中,但在发出请求后显示以下错误:
403
This request is not authorized to perform this operation using this permission.
{
'content-length': '279',
'content-type': 'application/xml',
server: 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0',
'x-ms-request-id': '4de6c154-f01e-0051-7ce4-1314ef000000',
'x-ms-version': '2018-03-28',
'x-ms-error-code': 'AuthorizationPermissionMismatch',
date: 'Mon, 08 Mar 2021 06:32:44 GMT',
connection: 'close'
}
upload/PUT文件的代码是:
const request = require("request");
require("dotenv").config();
const account = process.env.ACCOUNT_NAME || "";
const containerName = "demo";
const blobName = "dummyfile1.txt";
const blobContent = "Hello, This will be written in file";
const contentLength = new TextEncoder().encode(blobContent).length;
var strTime = new Date().toUTCString();
const options = {
url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}`,
headers: {
Authorization: "Bearer <BearerToken>",
"x-ms-date": strTime,
"x-ms-version": "2018-03-28",
"x-ms-blob-type": "BlockBlob",
"Content-Length": contentLength,
"Content-Type": 'application/text-plain',
},
body: blobContent,
};
function callback(error, response, body) {
console.log(response.statusCode);
console.log(response.statusMessage);
console.log(response.headers);
}
request.put(options, callback);
我在这里手动替换我通过 POSTMAN 获取的那个:
另外,我在应用中添加了Storage Data Contributor的权限:
我已将 Azure 存储、user_impersonation 权限也委托给了应用程序。
但是,同样的错误仍然存在。
【问题讨论】:
-
answer 使用客户端凭据流。但是你在这个问题中使用auth code flow。尝试添加Azure Storage permission 并使用
https://storage.azure.com/user_impersonation offline_access范围。 -
@Pamela 我尝试使用
https://storage.azure.com/user_impersonation offline_access通过 POSTMAN 访问承载令牌并使用该承载令牌来运行此请求。但是,它仍然给出了相同的错误消息。是的,同样在上一个问题中,我试图通过客户端凭据获取承载令牌,我试图通过身份验证代码流获取它。注意:我已将存储 Blob 数据贡献者提供给应用程序。 -
您应该在门户中添加 Azure 存储委托权限,请参阅 i.stack.imgur.com/XniAN.png。我试过你的代码,是正确的。
-
我已经将Azure Storage委派权限指定为user_impersonation,但还是同样的错误,我在问题更新中添加了截图。
-
这很奇怪。稍后我将添加有关访问令牌的详细步骤。
标签: javascript node.js azure azure-blob-storage bearer-token