当使用AD令牌/ Bearer令牌尝试在Azure Blob中上传文件时,出现授权权限不匹配的问题。[Azure-Blob][Bearer-Token]

3

我可以创建容器、列出容器和列出 blob,但是当我尝试向 Azure 存储的 Blob 中上传或删除文件时,进行 PUT/DELETE 请求后,它会显示以下错误:

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

上传/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获取的代码中的 < 的符号:

POSTMAN调用以获取访问令牌

此外,我还将Storage Data Contributor权限添加到了该应用程序中:

Access Control IAM

我还将Azure Storage、user_impersonation权限委派给了该应用程序。 Azure Storage user_impersonation

但问题仍然存在。


答案使用客户端凭据流。但是在这个问题中,您使用了授权码流。尝试添加Azure存储权限,并使用https://storage.azure.com/user_impersonation offline_access范围。 - unknown
@Pamela 我尝试使用 https://storage.azure.com/user_impersonation offline_access 在 POSTMAN 中访问 Bearer Token 并使用该令牌运行此请求。但仍然会出现相同的错误消息。 是的,在之前的问题中,我正在尝试通过客户端凭据获取Bearer令牌,而在这里我正在尝试通过Auth Code流程获取它。 注意:我已将Storage Blob数据贡献者授予了该应用程序。 - Mayank Patel
您应该在门户中添加Azure存储委派权限,请参见https://i.stack.imgur.com/XniAN.png。我已经尝试了您的代码,它是正确的。 - unknown
我已经将Azure Storage委派权限指定为user_impersonation,但仍然出现相同的错误,我已在问题更新中添加了截图。 - Mayank Patel
很奇怪。稍后我会添加有关访问令牌的详细步骤。 - unknown
@Pamela 我能够使用该 Bearer Token 列出容器(ListContainer)、列出 Blob(ListBlobs)和创建容器(CreateContainer)。但我无法创建 Blob、删除 Blob 和获取 Blob。 - Mayank Patel
2个回答

2
使用 授权码流程 时,需要为您已登录的用户授予 Azure 存储的权限。当使用 存储 Blob 数据参与者 角色时,您需要将角色分配添加到您的帐户而不是应用程序(只有客户端凭据流程需要应用程序角色)。
然后将 Azure 存储权限添加到 API 权限中。

enter image description here

此外,https://<account-name>.blob.core.windows.net/user_impersonationhttps://storage.azure.com/user_impersonation 均可用于范围(scope)。有关 Azure 存储资源 ID(scope)的更多详细信息,请参见此处
对于客户端凭据流(client credentials flow),适用的是 https://${account}.blob.core.windows.net/.defaulthttps://storage.azure.com/.default
步骤:
  1. 在浏览器中获取授权码

注意:当 Azure 帐户已登录时,您应该接受所请求的权限。

https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/authorize?
client_id={client-id}
&response_type=code
&redirect_uri=https://localhost:44300/
&response_mode=query
&scope=https://{account}.blob.core.windows.net/user_impersonation
&state=12345
&prompt=consent

获取访问令牌和刷新令牌。尝试在https://jwt.io/上解码访问令牌,检查aud,它看起来像https://xxxx.blob.core.windows.net

enter image description here

最后,在您的代码中测试访问令牌。

enter image description here


我能够使用Bearer Token列出容器(ListContainer)、列出Blob(ListBlobs)、创建容器(CreateContainer)。但是我无法创建Blob(Create a Blob)、删除Blob(Delete a Blob)、获取Blob(GetBlob),它们需要特殊的角色或权限吗? - Mayank Patel
你好,@MayankPatel。你是否为你的账户添加了“存储 Blob 数据读取器”角色? - unknown
不,我已经将“存储 Blob 数据参与者”添加为角色。如问题中的截图所示。 - Mayank Patel
这仅适用于应用程序,而不适用于已登录的用户。尝试将“存储 Blob 数据参与者”添加到用户,并在大约10分钟后进行测试。我已经在我的一侧添加了该角色!忘记检查它:( - unknown
我测试了没有这个角色,它返回了403。它需要太长时间才能生效。为用户帐户添加角色是非常重要的。 - unknown
我将角色分配给了我的已登录电子邮件ID,除了应用程序之外,它成功地运行了所有请求。谢谢。 - Mayank Patel

0
自从我遇到这个问题以来,请确保您的共享访问令牌(SAS)具有正确的权限。我在使用列出 Blob REST API 时,错误地使用了读取权限而不是列表权限。只是为了那些可能遇到这个问题的人们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接